Convert "1.79769313486232E+308" to double without OverflowException?

convert antonym
convert math
convert synonym
unit convert
convert in a sentence
google convert
convert money
please convert

I have this string "1.79769313486232E+308" and am trying to convert it to a .NET numeric value (double?) but am getting the below exception. I am using Convert.ToDouble(). What is the proper way to do this conversion?

OverflowException: Value was either too large or too small for a Double

The problem is likely due to the fact that Double.MaxValue was converted to a string, and when the string is output, not all the digits are output, instead it is rounded. Parsing this value overflows the double.

Using Double.TryParse and subsequently checking equality on the string "1.79769313486232E+308" in case of failure and substituting Double.MaxValue should be a quick workaround, if you need to keep the string the way it is.

EDIT: Of course, if you don't need to keep the string the way it is, use the Round Trip format specifier to produce the string in the first place, as Jon describes in his answer.

Convert for Windows – joshmadison.com, This free conversion calculator converts between common units of length, temperature, area, volume, weight, and time. In addition, explore hundreds of other  Definition of convert (Entry 1 of 2) 1 a : to bring over from one belief, view, or party to another They tried to convert us to their way of thinking.

Unfortunately this value is greater than double.MaxValue, hence the exception.

As codekaizen suggests, you could hard-code a test for the string. A better (IMO) alternative if you're the one producing the string in the first place is to use the "r" format specifier. Then the string you produce will be "1.7976931348623157E+308" instead, which then parses correctly:

string s = double.MaxValue.ToString("r");
double d = double.Parse(s); // No exception

Obviously that's no help if you don't have control over the data - but then you should understand you're likely to be losing data already in that case.

Conversion Calculator, Conversion of Units. Measurement Conversion. Don't know how to convert? The answer is here. Welcome to Convert. net Convert.net is an online conversions calculator for a variety of unit measurements featuring natural language queries. You can use Convert.net to sum up a list of numbers, convert from pounds to kilograms, or evaluate any other mathematical expression.

You may try double.Parse() or double.TryParse() rather than Convert.ToDouble(), but I'm not certain you'll get better results. Incidentally, the string that you provide is equal to double.MaxValue, which is (of course) the maximum value that can be contained in a double, so that's likely where your error is coming from. Floating-point numeric types are finicky, so I would assume that some sort of rounding is taking place and pushing it outside the bounds of the type.

You could also try the decimal data type. You may have better luck there.

Measurements Converter: Conversion of Weights and Measures, is a person who joins a cult and abandons his old religious beliefs. Convert is a free and easy to use unit conversion program that will convert the most popular units of distance, temperature, volume, time, speed, mass, power, density, pressure, energy, and many others, including the ability to create custom conversions! Download Convert v4.10 (it's free!).zip — 153 KB

Here is what I came up with. Thanks Jon Skeet and codekaizen.

private double convertToDouble(string str)
{
    double dbl;

    if (double.TryParse(str, out dbl))
        return dbl;

    if (str == "1.79769313486232E+308")
        return double.MaxValue;

    return double.MinValue;
}

Converted, rate would be 5%, since 50 ÷ 1,000 = 5%. Convert files like images, video, documents, audio and more to other formats with this free and fast online converter. Convert media free, fast and online.

Demonstrates the issue and a solution:

var s = double.MaxValue.ToString();
double d;
if (!double.TryParse(s, out d)) {
    d = s.Equals(double.MaxValue) ? double.MaxValue : double.MinValue;
}

convert, Convert definition, to change (something) into a different form or properties; transmute; transform. See more. OnlineVideoConverter.com (OVC) is a free online media conversion web application that allows you to convert any video link or file to various formats without the need to install any software on your computer.

convert defined, Convert files like images, video, documents, audio and more to other formats with this free and fast online converter. Conversion Calculator. Use this Conversion Calculator to convert between commonly used units. Select the current unit in the left column, the desired unit in the right column, and enter a value in the left column to generate the resulting conversion. A full list of unit conversions is available at unitconverters.net.

Conversion rate: Definition - Google Ads Help, Convert more visitors, plug revenue leaks, and save on testing tool costs with Convert Experiences. 4X faster support. Free trial available. The videos are always converted in the highest available quality. Please note that we can only convert videos up to a length of 1 hour - the limitation is necessary, so the conversion of any video will not take more than a couple of minutes. Our service is for free and does not require any software or registration.

Convert, Quick, free, online unit converter that converts common units of measurement, along with 77 other converters covering an assortment of units. The site also  Calculate live currency and foreign exchange rates with this free currency converter. You can convert currencies and precious metals with this currency calculator.

Comments
  • Or just use double.MaxValue directly? Or do a string test for that particular string and substitute double.MaxValue, since it's obviously a special case.
  • Decimal won't work at all. Decimal.MaxValue < Double.MaxValue.
  • This is WRONG solution! If TryParse returns false, then the result value is 0.