Re: Convert float to gchar *



"Laurent Houdusse" wrote:

I need to convert float in gchar*
is there a function in glib?

see my code:
void RefreshFramePerSecond(void)
{
      float fNbFps;
      fNbFps = oDw3D.GetNbFps();
      gtk_window_set_title(GTK_WINDOW(oWindow),fNbFps);//error to
      convert float to const char*
}

snprintf () is your friend (while sprintf () is your foe, btw!). Here is
a simple example:

void set_title_func (void) {
    char title_buffer[16];
    snprintf (title_buffer, sizeof (title_buffer), "%f", your_float);
    gtk_window_set_title (your_window, (const gchar *) title_buffer);
}

I'm not sure whether the cast is correct and working with every compiler
atm (wasn't testing this example) but it should be at least something
similar. Works without cast as well but produces a compiler warning.

Using snprintf () you can also compose complex window titles (text,
floats, ints and even substrings mixed). Just chose the size of the
title_buffer[] accordingly. Have a look again at the snprintf ()
documentation to discover its various capabilities, for instance at
http://www.die.net/doc/linux/man/man3/snprintf.3.html

Btw, GLib contains its own variant named g_snprintf ().
http://developer.gnome.org/doc/API/2.0/glib/glib-String-Utility-Functions.html#g-snprintf



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