In C# (.Net 7):
float a = 13082987520.00f;
Console.WriteLine(a);
Console.WriteLine($"{a:N2}");
Console.WriteLine($"{a:0.00}");
I get:
1.3082988E+10
13,082,987,520.00
13082990000.00
In particular, notice the numerical difference between the last two lines:
13082987520
vs
13082990000
It's causing a lot of confusion when I try to write such numbers as CSV files. In this expected behaviour? How can we explain it?
Notice this question is VERY DIFFERENT FROM Difference between ToString("N2") and ToString("0.00"). I know the difference in terms of formatting, but here I am pointing out a potential unexpected implementation detail (and potentially a bug?) of floating point number printing. As shown in above case, apparently "N2" is NOT JUST adding thousand separators while "0.00" won't.
The same problem doesn't exist for double:
double b = 13082987520.00;
Console.WriteLine(b);
Console.WriteLine($"{b:N2}");
Console.WriteLine($"{b:0.00}");
13082987520
13,082,987,520.00
13082987520.00
The question here is:
- Which is the "correct" one for (single) floating point? (Actually it's the one printed with "N2")
- How do I print the correct one (which is the one shown using "N2") without having to do
a.ToString("N2").Replace(",", string.Empty)
?