try_parse
Description:
[ Version ( since = "2.12" ) ]
public static bool try_parse (string str, out int64 result = null, out unowned string unparsed = null, uint _base = 0)
public static bool try_parse (string str, out int64 result = null, out unowned string unparsed = null, uint _base = 0)
Converts a string to a gint64 value.
If the correct value would cause overflow, int64.MAX or int64.MIN is returned, and ERANGE is stored in errno. If the base is outside the valid range, zero is returned, and ERANGE is stored in errno. If the string conversion fails, zero is returned.
Example: String to int64, with error detection:
public static int main (string[] args) {
int64 val;
bool res;
// Output: ``true => 34``
res = int64.try_parse ("34", out val);
print ("%s => %" + int64.FORMAT + "\n", res.to_string (), val);
// Output: ``true => -34``
res = int64.try_parse ("-34", out val);
print ("%s => %" + int64.FORMAT + "\n", res.to_string (), val);
// Output: ``false => 0``
res = int64.try_parse ("d34", out val);
print ("%s => %" + int64.FORMAT + "\n", res.to_string (), val);
// Output: ``false => 34``
res = int64.try_parse ("34d", out val);
print ("%s => %" + int64.FORMAT + "\n", res.to_string (), val);
return 0;
}
valac --pkg glib-2.0 int64.try_parse.vala
Parameters:
str |
the string to convert to a numeric value. |
result |
the location to store the value |
Returns:
true on success, or false. |