双的ToString - 没有科学记数法 [英] Double ToString - No Scientific Notation

查看:128
本文介绍了双的ToString - 没有科学记数法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是碰到了.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?

我见过的<一个href="http://stackoverflow.com/questions/1546113/double-to-string-conversion-without-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).

这篇关于双的ToString - 没有科学记数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆