Re: Glib::ustring conversion



Arthur wrote:

Hi there.

I'm building a simple calculator with Glade(mm) and I would like to know if it is possible to convert Glib::ustring to double and then double to Glib::ustring. I need this because the calculator has an entrytext widget and when I press the numbers and signs (plus, minus, multiply, divide, left parenthesis and right parenthesis) a string is shown at this entry text: "1254 + 13241 - (13241 * 1341)". When the equal button (=) is pressed I want to pass this string as a double and make C++ to solve this mathematic expression for me and return the result, so I can set it as the entry text string again.

If anyone could help me, I would appreciate.

Regards,

Arthur
_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
http://mail.gnome.org/mailman/listinfo/gtkmm-list

I don't know of any library function in the standard C library that would do what you describe. However, glib has a pair of routines: g_ascii_strtod() and g_ascii_dtostr() (see docs <http://developer.gnome.org/doc/API/2.0/glib/glib-String-Utility-Functions.html#g-ascii-strtod> for syntax). They will convert ascii strings to doubles, and doubles to ascii strings...

Unfortunately, you cannot pass the entire string you'll get back from your from your Gtk::Entry widget (Glib::ustring::c_str()) to g_ascii_strtod and expect the answer to your calculation. You will have to parse your string and use g_ascii_strtod to convert all the number strings and perform the calculations yourself. Depending on the level of the calculation and how deeply nested your expression is, that would not be a trivial undertaking.

Bob



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