parse


Description:

public static double parse (string str)

Converts a string to a double value.

This function behaves like the standard strtod() function does in the C locale. It does this without actually changing the current locale, since that would not be thread-safe. A limitation of the implementation is that this function will still accept localized versions of infinities and NANs.

This function is typically used when reading configuration files or other non-user input that should be locale independent. To handle input from the user you should normally use the locale-sensitive system strtod() function.

If the correct value would cause overflow, plus or minus HUGE_VAL is returned (according to the sign of the value), and ERANGE is stored in GLib.errno. If the correct value would cause underflow, zero is returned and ERANGE is stored in GLib.errno.

This function resets errno before calling strtod() so that you can reliably detect overflow and underflow.

Example: String to double:

public static int main (string[] args) {
// Output: ``10.1235``
print ("%g\n", double.parse ("10.12345"));

// Output: ``-0.12345``
print ("%g\n", double.parse ("-0.12345"));

// Output: ``0``
print ("%g\n", double.parse ("d.12345"));

// Output: ``0.12345``
print ("%g\n", double.parse ("0.12345D"));
return 0;
}

valac --pkg glib-2.0 double.parse.vala

Parameters:

str

the string to convert to a numeric value.

Returns:

the guint64 value or zero on error.