I'm trying to convert a float to it's string representation without the result appearing in scientific notation.
I first tried :
ToString("0." + new string('#', 7))
but this doesn't seem to work for large values. For example :
float largeNumber = 12345678f;
string str = largeNumber.ToString("0." + new string('#', 7));
results in "12345680"
I then tried ToString("R")
this works for the large number above, but if the numbers get too large, it displays them in scientific notation. For example 5000000000f
results in "5E+09"
. And small numbers like 0.0005
result in 0.0004999999966
I've also tried mixing the 2, but I still get scientific notation in some cases.
My test program is pasted below. I appreciate that there will be precision issues, but I'm wondering if I can do any better than what I have?
class Program
{
static void Main(string[] args)
{
Write(0.123456789f);
Write(0.12345678f);
Write(0.1234567f);
Write(0.123456f);
Write(0.12345f);
Write(0.1234f);
Write(0.123f);
Write(0.12f);
Write(0.1f);
Write(1);
Write(12);
Write(123);
Write(1234);
Write(12345);
Write(123456);
Write(1234567);
Write(12345678);
Write(123456789);
Console.WriteLine();
float f = 5000000000f;
for (int i = 0; i < 17; ++i)
{
Write(f);
f /= 10;
}
Console.WriteLine();
f = 5000000000f;
for (int i = 0; i < 17; ++i)
{
Write(f < 1 ? f + 1 : f);
f /= 10;
}
Console.Read();
}
static void Write(float f)
{
//string str = f.ToString("0." + new string('#', 7));
//string str = f.ToString("R");
string str = Math.Abs(f) < 1 ? f.ToString("0." + new string('#', 7)) : f.ToString("R");
Console.WriteLine(str);
}
}
The
0
in the format string is not for zeroes, it represents "digit if non-zero and zero if zero", while '#' represents "digit if non-zero or nothing if zero".You can use the format
f.ToString("0." + new string('#', 7))
for numbers over zeroI just tested it in PowerShell and it works fine for me, although it's probably using doubles or decimals:
Definitely, the problem seems to be
float
precision: