具有最大字符的System.Double值(System.Double value with max characters)

我正在测试double[]数组的net xml序列化,所以我很想知道什么是具有大多数字符的双重值,它是序列化的,所以我可以测试序列化数组的最大输出大小。

I'm testing net xml serialization of double[] arrays so I'm interested to know whats the double value that has most characters int it's serialized for so I can test whats the max output size of serialized array.

最满意答案

它应该是24。

double.MinValue.ToString("R").Length

来自double.ToString(string)

或者“R”,如果数字可以用该精度表示,则返回15位数;如果数字只能用最大精度表示,则返回17位数。

你有最多17位数,加1表示符号,加1为小数分隔符加5表示E + xxx( double.MaxValue为1.7976931348623157E+308和double.Epsilon ,最小值> 0 ,是4.94065645841247E-324 ,所以形式为E[+-][0-9]{1,3} )。

请注意,在技术上,在某些奇怪的语言中,

var str2 = double.PositiveInfinity.ToString("R");

可能会更长(因为字符串已本地化),但我希望您使用CultureInfo.InvariantCulture序列化您的数字!

但请记住,用户可以从控制面板改变他们的文化...类似于:

var culture = (CultureInfo)CultureInfo.CurrentCulture.Clone(); culture.NumberFormat.NegativeSign = "Negative"; culture.NumberFormat.NumberDecimalSeparator = "DecimalSeparator"; var str4 = double.MinValue.ToString("R", culture);

结果: Negative1DecimalSeparator7976931348623157E+308

出于这个原因,最好使用CultureInfo.InvariantCulture :-)

但是如果你想知道真相,在控制面板中小数点分隔符可以长达3个字符,负号表示最多4个(你可以尝试它,或者你可以检查LOCALE_SDECIMAL和LOCALE_SNEGATIVESIGN ,显然终止为null在.NET中可以忽略字符)

It should be 24.

double.MinValue.ToString("R").Length

From double.ToString(string)

or "R", which returns 15 digits if the number can be represented with that precision or 17 digits if the number can only be represented with maximum precision.

you have that there are at max 17 digits, plus 1 for sign, plus 1 for the decimal separator, plus 5 for the E+xxx (double.MaxValue is 1.7976931348623157E+308 and double.Epsilon, the smallest value > 0, is 4.94065645841247E-324, so both in the form E[+-][0-9]{1,3}).

Note that technically, in some strange languages,

var str2 = double.PositiveInfinity.ToString("R");

could be longer (because the string is localized), but I hope you'll serialize your numbers with CultureInfo.InvariantCulture!

But remember that users could have changed their culture from the control panel... something like:

var culture = (CultureInfo)CultureInfo.CurrentCulture.Clone(); culture.NumberFormat.NegativeSign = "Negative"; culture.NumberFormat.NumberDecimalSeparator = "DecimalSeparator"; var str4 = double.MinValue.ToString("R", culture);

Result: Negative1DecimalSeparator7976931348623157E+308

For this reason it's better to use the CultureInfo.InvariantCulture :-)

But if you want to know the truth, in the Control Panel the decimal separator can be long up to 3 characters, and the negative sign up to 4 (you can try it, or you can check the LOCALE_SDECIMAL and LOCALE_SNEGATIVESIGN, clearly the terminating null character can be ignored in .NET)

更多推荐