如何在.NET Framework中将一个double转换为没有科学记数法的浮点字符串表示形式?
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
标准数字格式都不是这样的,而且自定义格式 似乎也不允许开放位数在小数点分隔符之后.
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.
这不是 如何在没有 10 表示的幂的情况下将双精度数转换为字符串 (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).
问题实际上只是关于替换上述数字.我知道浮点数的工作原理以及可以使用和计算哪些数字.
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.
对于通用¹解决方案,您需要保留 339 个位置:
For a general-purpose¹ solution you need to preserve 339 places:
doubleValue.ToString("0." + new string('#', 339))
非零小数位数的最大数量为 16.15 在小数点右侧.指数最多可以将这 15 位数字向右移动 324 位.(查看范围和精度.)
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 代码一次性完成的.此外,代码更容易证明是正确的.
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.###################################################################################################################################################################################################################################################################################################################################################";
}
¹ 更新:我误以为这也是无损解决方案.事实上它不是,因为 ToString
对除了 r
之外的所有格式进行正常的显示舍入.活生生的例子.谢谢,@讨厌!如果您需要以定点表示法往返的能力(即,如果您使用 .ToString("r")
今天).
¹ Update: I mistakenly said that this was also a lossless solution. In fact it is not, since ToString
does its normal display rounding for all formats except r
. Live example. Thanks, @Loathing! Please see Lothing’s answer if you need the ability to roundtrip in fixed point notation (i.e, if you’re using .ToString("r")
today).
这篇关于没有科学记数法的双精度字符串转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!