locale affects (infects? ;) strtod()




hi folks,

while including GScanner into glib, i ran into an obscure
problem ;(
the strtod() function <stdlib.h> got unusable for the
string->float conversion within the lexer code, because it
doesn't recognize the `.' anymore:

#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>

main (int argc, char *argv[])
{
	gchar *number = "1.0";
	gchar *string;
	gfloat x;
	
	gtk_init (&argc, &argv);

	x = strtod ("1.1", &string);
	if (string && *string)
	  printf ("strtod(\"%s\") failed at: `%s'\n", number, string);
	else
	  printf ("strtod(\"%s\") gave: %f\n", number, x);
	
	printf ("setting locale...\n");
	gtk_set_locale ();
	
	x = strtod ("1.1", &string);
	if (string && *string)
	  printf ("strtod(\"%s\") failed at: `%s'\n", number, string);
	else
	  printf ("strtod(\"%s\") gave: %f\n", number, x);
}

now this little program gave me:

birgrave:bash<~>$ gcc main.c -lgtk -lgdk -lglib -lX11 -lm && ./a.out
strtod("1.0") gave: 1.100000
setting locale...
strtod("1.0") failed at: `.1'
birgrave:bash<~>$

if that's of interest, $LANG is set to `de_DE.88591' by default on
my system.

if strtod() can't be used to parse _international_ *rc files, which 
function should be used instead to do string->float conversions?


---
ciaoTJ



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