As I understand from Microsoft's documentation, if I have 340550.46 and I apply the format string #,##0.00 I should get the string "340,550.46". But instead, I'm getting "340.550,50". I've set breakpoints to ensure that I know what the number and format strings are going in.
To be more specific, I have a string "340550.46", and I convert it to a float, and then use ToString() to format it with "#,##0.00":
float.Parse("340550.46").ToString("#,##0.00)
What am I doing wrong here? Why is it rounding the number, and what can I do to prevent that?
CodePudding user response:
The reason for the rounding is that a float
is limited to 7 signficant digits.
See: https://en.wikipedia.org/wiki/IEEE_754
If you changed to a double
, you would get that 8th extra digit. Also, if you are formatting to 2 decimal places since it may be currency, consider using Decimal
.