我只是碰到了.NET会的默认的DO Double.ToString()
用科学记数法,如果有精彩的功能足够的小数位。
I just came across the wonderful "feature" that .NET will by default do Double.ToString()
using scientific notation if there are enough decimal places.
.005.ToString() // ".005"
.00005.ToString() // "5E-05"
有谁知道一个有效的方法来获得这个显示为一个字符串的标准(非科学)的符号?
Does anyone know an efficient way to get this to appear as a string in standard (non-scientific) notation?
我见过的this问题,但前两个答案似乎有点哈克给我,因为他们俩都 Double.ToString()
,然后只需重新格式化的结果。
I've seen this question, but the top two answers seem kind of hacky to me as they both do Double.ToString()
and then just reformat the results.
感谢您的帮助。
编辑:
所需的输出:
.00005.ToString() == ".00005"
EDIT2:
什么是与所有的重复和密切票?我特别要说的的问题的类似的问题并没有一个满意的答复。本网站上的人们得到了太多权力开心。
What is with all the duplicate and close votes? I specifically say in the question that the similar question does not have a satisfactory answer. People on this website get way too power happy.
我的解决办法:
在情况下,它是有用的人:
In case it's useful to anyone:
/// <summary>
/// Converts the double to a standard notation string.
/// </summary>
/// <param name="d">The double to convert.</param>
/// <returns>The double as a standard notation string.</returns>
public static String ToStandardNotationString(this double d)
{
//Keeps precision of double up to is maximum
return d.ToString(".#####################################################################################################################################################################################################################################################################################################################################");
}
请注意:这仅> 1。我还没有找到一个有效的方法来做到这一点对于所有的值还适用于值
NOTE: This only works for values > 1. I haven't found an efficient way to do this for all values yet.
推荐答案
查看标准和自定义数字格式字符串的。我认为你正在寻找
################。
See the Standard and Custom Numeric Format Strings. I think you're looking for ".################"
:
.00005.ToString(".################") // returns ".00005"
这有点hackish的,是的,但它的工作原理,只要你没有超过小数点后的很多地方。如果你这样做,你可能想使用的StringBuilder
来打造出约330 #
在你的字符串( double.Epsilon
是的顺序E-324)。
It's somewhat hackish, yes, but it works, as long as you don't have more than that many places after the decimal. If you do, you might want to use a StringBuilder
to build out about 330 #
s in your string (double.Epsilon
is on the order of E-324).
相关推荐
最新文章