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

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

问题描述

.NET Framework 中如何将 double 转换为没有科学记数法的浮点字符串表示形式?

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

小"样本(有效数字可以是任意大小,例如 1.5E2001e-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 次幂的情况下将 double 转换为字符串 (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.Epsilondouble.MinValuedouble.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).

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

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