开发者

Convert double string back to double

开发者 https://www.devze.com 2023-02-21 01:31 出处:网络
var doubleMaxValueString = double.MaxValue.ToString(CultureInfo.InvariantCulture); and I want to convert it back to double, so I\'m doing like this:
var doubleMaxValueString = double.MaxValue.ToString(CultureInfo.InvariantCulture);

and I want to convert it back to double, so I'm doing like this:

var doubleMaxValue = double.Parse(doubleMaxValueString, CultureInfo.InvariantCulture);

but it doesn't work...

how can I convert it back to double?

thanks!

开发者_StackOverflow

Edit: it throws: OverflowExceptionValue: was either too large or too small for a Double.

I'm using var because it's more practical =)


You can find good explanation here. Basically the problem is that while converting double to string we're rounding it up so it exceeds double.MaxValue. You can fix for example in this way:

var doubleMaxValueString = double.MaxValue.ToString("R", CultureInfo.InvariantCulture);
var doubleMaxValue = double.Parse(doubleMaxValueString, CultureInfo.InvariantCulture);

More information on "R" argument and why it helps may be found here on msdn

0

精彩评论

暂无评论...
验证码 换一张
取 消