2

I have a very strange problem which is occurring on only one pc out of my many customers. I have tracked it down to the the Math.Round method.

On this brand new win7 pc Math.Round(1.59999,2) returns 1.59! It should be 1.6 and it is in all other cases.

I understand the quirks of Math.Round with regard to bankers rounding and I'm also aware of the various midpointrounding options in the overloads, but in any case 1.59999 should certainly always be 1.6.

We have tried repairing the .NET Framework (3.5 sp1) on the PC and doing a full virus scan. What could be the cause of this behavior and how might I further investigate the problem.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 1
    What differences did you find on that specific computer? Regional settings? Currency, country settings, decimals and such. – Independent Jul 18 '12 at 09:30
  • @Jonas. Yes, I did check the regional settings and all appear to be default UK. – StructSoftDev Jul 18 '12 at 09:40
  • @Tim. Thanks for the edit on my question Tim. New to SO, so just learning the correct format to post. – StructSoftDev Jul 18 '12 at 09:43
  • @StructSoftDev: Welcome to SO. Here are more informations how to format properly: http://meta.stackexchange.com/a/22189/147438 – Tim Schmelter Jul 18 '12 at 09:44
  • Perhaps its less to do with the behaviour of `Math.Round` and more to do with precision of `double` – Jodrell Jul 18 '12 at 10:08
  • 2
    Please post a complete example, including your conversion to string. Posting the *exact* value before and after rounding would be useful too. – CodesInChaos Jul 18 '12 at 10:11
  • Probably relevant: [C# rounding differently depending on platform?](https://stackoverflow.com/q/47302758/11683) – GSerg Nov 18 '19 at 09:43

2 Answers2

1

The first thing I would look at is where the number 1.59999 is coming from.

Is it the result of a string which is being parsed to a float before sending to Math.Round? If so, could it be that that string is being truncated? Can you be sure that the value passed to Math.Round is in fact 1.59999 and not 1.59?

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • Hi Jamiec, Thanks for your response. I'm certain that the value being passed to the math.round is not truncated. I placed a condition in the software to check for this bad rounding and output the values of the variable involved to the event viewer. Any other ideas? – StructSoftDev Jul 18 '12 at 09:37
1

Your core issue is that you're double but you expect meaningful decimal digits. If decimal digits are meaningful,

I also suspect that the main difference isn't introduces when rounding: You'll always get a value very close to, but not identical to 1.6. 1.6 is not representable exactly with double. It's possible you only get slightly different values on different PCs, especially if some other dll in the same process changed the floating point flags.

I suspect that the observed difference happens in your conversion to string.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • I suspect the same but, its hard to tell with certainty from the question. – Jodrell Jul 18 '12 at 10:12
  • Thank you guys. For the string representation of the return values from `Math.Round` I used both `.ToString` which produced "1.59". I also used `.ToString("0.00")`, which should round any non exact double of `1.6` to produce the string value of "1.60", but it doesn't - I still get "1.59". I will investigate some more on the line of double to string conversion. – StructSoftDev Jul 18 '12 at 10:50