Search code examples
cfloating-pointprintfgtk

GTK fprint(%e) float writted with commas instead of dots


So, I'm creating a little solar system simulation, with calculus done in C. Once the calculations are done I put them into a json file, which is read by a web page.

I have created a function to save the coordinate of the trajectory into a json file, all coordinates are double. To print the double into my json file I use fprintf with %e (yes, I want the scientific writing).

FILE *fp = NULL;
fp = fopen("file.json", "w");    
fprintf(fp, "[[%e, %e, %e],[%e, %e, %e], %d],\n", pos.x, pos.y, pos.z, speed.x, speed.y, speed.z, time);

My problems is that, when I use the function with my main.c, it writes the double with dots as I want (ex: 3.14), but when I use the function with Gtk it writes them with commas (ex: 3,14).

I tried to use g_fprintf built-in fprintf function by GTK, but I have the same problem. Any idea how to solve this ?

(If you need more info about the code or the problems, I am at your dispositions)


Solution

  • gtk alters locales settings, switch to default with setlocale just after calling gtk_init

    #include <locale.h>
    
    int main(int argc, char *argv[])
    {
        gtk_init(&argc, &argv);
        setlocale(LC_NUMERIC, "C");
    

    Or you can call gtk_disable_setlocale(); at the very beginning before gtk_init() or gtk_application_new()