双未经科学记数法字符串转换 [英] Double to string conversion without scientific notation

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

问题描述

如何转换出双入浮点字符串再presentation没有在.NET Framework科学记数法?

How to convert a double into a floating-point string representation without scientific notation in the .NET Framework?

小的样本(有效数字可以是任意大小,如 1.5E200 1E-200 ):

"Small" samples (effective numbers may be of any size, such as 1.5E200 or 1e-200) :

3248971234698200000000000000000000000000000000
0.00000000000000000000000000000000000023897356978234562

没有的标准数字格式都是这样的,和的custom格式也似乎并没有让具有小数点后位数的开放数量。

None of the standard number formats are like this, and a custom format also doesn't seem to allow having an open number of digits after the decimal separator.

这是不是<一个副本href="http://stackoverflow.com/questions/1319191/how-to-convert-double-to-string-without-the-power-to-10-re$p$psentation-e-05">How转换双串无电源10重新presentation(E-05),因为那里给出的答案做的不可以解决手头的问题。在这个问题上被接受的解决方案是使用一个固定的点(如20位),这不是我想要的。固定点格式和修剪多余的0不能解决问题或者是因为最大宽度固定宽度是99个字符。

This is not a duplicate of How to convert double to string without the power to 10 representation (E-05) because the answers given there do not solve the issue at hand. The accepted solution in this question was to use a fixed point (such as 20 digits), which is not what I want. A fixed point formatting and trimming the redundant 0 doesn't solve the issue either because the max width for fixed width is 99 characters.

注意:的解决方案必须与自定义数字格式正确处理(例如:其他小数点分隔符,这取决于文化信息)

Note: the solution has to deal correctly with custom number formats (e.g. other decimal separator, depending on culture information).

编辑:的问题是,真的只有约displaing上述数字。我所知道的是如何浮点数工作,哪些号码可以使用,并与他们进行计算。

The question is really only about displaing aforementioned numbers. I'm aware of how floating point numbers work and what numbers can be used and computed with them.

推荐答案

有关无损,通用的解决方案,你需要preserve 339地方:

For a lossless, general-purpose solution you need to preserve 339 places:

doubleValue.ToString(0+新的字符串('#',339))

非零十进制数的最大数量是16。15是在右侧的小数点。该指数可以将这些15位的最大的324位到右侧。 (见的范围和precision。

The maximum number of non-zero decimal digits is 16. 15 are on the right side of the decimal point. The exponent can move those 15 digits a maximum of 324 places to the right. (See the range and precision.)

它适用于 double.Epsilon double.MinValue ,的 double.MaxValue ,以及任何在两者之间。

It works for double.Epsilon, double.MinValue, double.MaxValue, and anything in between.

性能会比正则表达式/字符串操作的解决方案,因为所有的格式和字符串的工作是在一个路过的非托管的CLR code做的更大。此外,code就简单多了,证明是正确的。

The performance will be much greater than the regex/string manipulation solutions since all formatting and string work is done in one pass by unmanaged CLR code. Also, the code is much simpler to prove correct.

有关易用性和更好的性能,使其成为一个恒定的:

For ease of use and even better performance, make it a constant:

public static class FormatStrings
{
    public const string DoubleFixedPoint = "0.###################################################################################################################################################################################################################################################################################################################################################";
}

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

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