Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AT - Rounding differences between Turnover counter and QR Code #132

Open
StefanKert opened this issue May 10, 2023 · 0 comments
Open

AT - Rounding differences between Turnover counter and QR Code #132

StefanKert opened this issue May 10, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@StefanKert
Copy link
Member

StefanKert commented May 10, 2023

Currently we do round turnover and qrcode numbers differently. While we are rounding the summed up values for the turnover:

but we are rounding each value for the qrcode.

sb2.AppendFormat(System.Globalization.CultureInfo.CreateSpecificCulture("de-AT"), "{0:0.00}_{1:0.00}_{2:0.00}_{3:0.00}_{4:0.00}_", betrag_Normal, betrag_Erm1, betrag_Erm2, betrag_Null, betrag_Besonders);

While this usually doesn't make a huge difference there are some cases that lead to diverging turnovercounters:

Betrag_normal: 41,305 
Betrag_Null: 0,205 

Turnover = 41,305 + 0,205 = 41,51 => Round => 41,51

QRCode = 41.31_0.00_0.00_0.21_0.00_ => Total Sum 41,52

In addition to that string.Format and Math.Round do behave differently

The main difference between the two methods lies in their rounding mechanisms. Here are the details:

  • string.Format("{0:F2}", 0.205): This function will round the number 0.205 to 2 decimal places, resulting in 0.21. The reason behind this is that the string.Format method uses banker's rounding (or round half to even), which is a type of rounding where the number rounded to the nearest even number if it lies exactly halfway between two numbers. In this case, 0.205 is exactly between 0.20 and 0.21, so it rounds up to 0.21 since the number after the second decimal place is 5 or more.

  • Math.Round(0.205, 2): This function rounds to the nearest number as well, but it uses what is called "away from zero" rounding. However, due to limitations in floating point precision, 0.205 might be represented internally as slightly less than 0.205 (something like 0.20499999999999996). So when you call Math.Round(0.205, 2), it may actually round down to 0.20 because the internal representation is slightly less than 0.205.

These methods can produce different results due to the different rounding strategies they use and the limitations of floating point precision. It's always important to consider these factors when deciding which method to use for rounding numbers in your C# programs.

@volllly volllly added the bug Something isn't working label Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants