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.

有帮助吗?

解决方案

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)

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top