Reputation: 371
I am working with GTK, and in my program I am taking a user input, converting it from a string to a double using atof() in order to make computations, and then outputting the result into an entry. My problem is when using
void gtk_entry_set_text( GtkEntry *entry, const gchar *text );
I obviously need to pass the second argument as a gchar (or just a regular pointer to an array of characters), but I am not sure how to convert my double back into a human readable representation of the double. For example, user enters 65.0. I convert the string "65.0" to a double 65.0, perform my function (say multiply by two), and now I need to convert the double 130.0 into "130.0", and then store it into a character array to be passed into gtk_entry_set_text. How can I achieve this?
To help clarify as much as possible, here is the code in question.
/* function for calculating optimal amount of cash to bet */
static void calculateRatioOfCash()
{
const gchar *userInput;
char outputString[BUFSIZ];
/* retrieve probability of winning from input field */
userInput = gtk_entry_get_text(GTK_ENTRY(entry));
/* convert our probability in str to a double and compute betPercentage */
double betPercentage = 2*(atof(userInput)) - 100;
outputString = /* what code goes here ?? */
gtk_entry_set_text(GTK_ENTRY(outputField), outputString);
}
Upvotes: 0
Views: 231
Reputation: 14607
Since you are using GLib already, I'd suggest using GLib string utility functions. In this case:
char *output = g_strdup_printf ("%f", betPercentage);
This is easier than using sprintf() because it allocates memory for you -- of course it means you should g_free (output)
when you're done with it.
Upvotes: 1