How to get floats value without including exponential notation

寵の児 提交于 2019-11-30 12:30:01

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.#########"));
nan
string dum = string.Format("{0:f99}",dummy).TrimEnd('0');
if (dum.EndsWith(",")) dum = dum.Remove(dum.Length - 1);

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.

Console.WriteLine(dummy.ToString("N5"));

where 5 its number of decimal places

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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!