双重ToString - 没有科学符号 [英] Double ToString - No Scientific Notation

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

问题描述

我刚刚遇到了这个精彩的功能,默认情况下,.NET将 使用科学记数法(如果有的话)使用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" 

有没有人知道一个有效的方法来使其在标准(非-scientific)符号?

Does anyone know an efficient way to get this to appear as a string in standard (non-scientific) notation?

我看过这个问题,但是顶部的两个答案对我来说似乎是一种黑客,因为他们都是 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"

有点黑客,是的,但它的作品,只要你没有超过那么多地方小数。如果你这样做,你可能想要使用一个 StringBuilder 在你的字符串中构建大约330 s( 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天全站免登陆