fromStringOrThrow Summary Converts a string to a valid double value or throws an exception if the value cannot be converted. Usage public static double fromStringOrThrow(string numberAsStr) Returns The double equivalent of the string value if the string is a valid double value (the string value fits within the double data range). Parameters numberAsStr The string value to convert. Description Converts a string to a valid double value or throws an exception if the value cannot be converted. If the string value is not numeric, System.Exceptions.InvalidNumberException will be thrown. If the string value is outside the range of valid values for the double data type, System.Exceptions.IntegerOutOfRangeException is thrown. For string values that are not numeric, System.Exceptions.InvalidNumberException is thrown: import System; import System.Exceptions; try { double x = Double.fromStringOrThrow("100"); } catch(InvalidNumberException e) { // Will not execute because the string value is numeric Console.error(e); } try { double y = Double.fromStringOrThrow("abc"); } catch(InvalidNumberException e) { Console.error(e); } However, if the Double.NaN (IEEE-754 Not a Number) value is desired, a string value of NaN will return this value and not throw: import System; double y = Double.fromStringOrThrow("NaN"); Console.log(y); If the string being converted is numeric but does not fit into the double data range (-1.79769313486232e308 to 1.79769313486232e308), System.Exceptions.DoubleOutOfRangeException will be thrown. import System; import System.Exceptions; try { double tooBig = Double.fromStringOrThrow("1e400"); } catch(DoubleOutOfRangeException e) { Console.error(e); } Examples Valid Conversion (No exception) 12345import System;import System.Exceptions; double valid = Double.fromStringOrThrow("123");Console.log(valid); // 123 Invalid Conversion (Not numeric) 12345678import System; try { double invalid = Double.fromStringOrThrow("abc");}catch(InvalidNumberException e) { Console.error(e);} Invalid Conversion (Out of range) 123456789import System;import System.Exceptions; try { double exceedByOne = Double.fromStringOrThrow("1e400");}catch(DoubleOutOfRangeException e) { Console.error(e);} Share HTML | BBCode | Direct Link