In C#, is it possible to perform ToString on a float and get the value without using exponentials?
For example, consider the following:
float dummy;
Without some further background info, it's hard to tell - but it sounds like you want decimal semantics. So why not use the decimal
type instead?
decimal dummy;
dummy = 0.000006M;
The decimal type is more accurate at representing decimal numbers than float
or double
, but it is not as performant. See here for more info.
float dummy = 0.000006F;
Console.WriteLine(dummy.ToString("0." + new string('#', 60)));
If you'll be doing this a lot then it makes sense to store the format string in a static
field/property somewhere and re-use it, rather than constructing a new string
every time:
private static readonly string _allFloatDigits = "0." + new string('#', 60);
// ...
float dummy = 0.000006F;
Console.WriteLine(dummy.ToString(_allFloatDigits));
Try this
Console.WriteLine(dummy.ToString("F"));
You can also specify number of decimal places. For example F5, F3, etc.
Also, you can check custom format specifier
Console.WriteLine(dummy.ToString("0.#########"));
Console.WriteLine(dummy.ToString("N5"));
where 5 its number of decimal places
string dum = string.Format("{0:f99}",dummy).TrimEnd('0');
if (dum.EndsWith(",")) dum = dum.Remove(dum.Length - 1);