System.Double value with maximum characters

I am testing the serialization of the double[] grid on the xml network, so I am interested in knowing what the double value is that most of the characters that it serialized have, so I can check what the maximum output size of the serialized array is.

+4
source share
2 answers

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 this precision, or 17 digits if the number can be represented only with maximum precision.

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

Please note that technically in some unfamiliar languages

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

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

But remember that users could change 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 is better to use CultureInfo.InvariantCulture

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

+5
source

You can try -1.0 / 3.0 , it will have many decimal places.

0
source

Source: https://habr.com/ru/post/1500517/


All Articles