[Vala] How to convert a string to float ?



Hello,

Just as I said in subject of this mail, I'm trying to convert a string
into a float.

To convert a string to int : int.parse
ok, easy...

Float... ehm.. no atof function, I've seen a lot of things about "Value"
so I've tried and I've got " GLib-GObject-CRITICAL **:
g_value_set_*******"...

So how can I convert ?

Sample code :
Value val = Value (typeof (double));
val.set_string("3.4");   // <---- error
stdout.printf ("get_double()\n");
stdout.printf ("%f\n", val.get_double());
stdout.printf ("get_string()\n");
stdout.printf ("%s\n", val.get_string()); // <-- error

Value val2 = Value (typeof (string));
val2.set_string("3.4");
stdout.printf ("get_double()\n");
stdout.printf ("%f\n", val2.get_double()); // <-- error
stdout.printf ("get_string()\n");
stdout.printf ("%s\n", val2.get_string());

OUTPUT :
---------------
(spawn.exe:4056): GLib-GObject-CRITICAL **: g_value_set_string: assertion
`G_VALUE_HOLDS_STRING (value)' failed
get_double()
0.000000
get_string()

(spawn.exe:4056): GLib-GObject-CRITICAL **: g_value_get_string: assertion
`G_VALUE_HOLDS_STRING (value)' failed
(null)
get_double()

(spawn.exe:4056): GLib-GObject-CRITICAL **: g_value_get_double: assertion
`G_VALUE_HOLDS_DOUBLE (value)' failed
0.000000
get_string()
3.4
-------------------


Thanks

Regards

Raum


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]