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

Time:06-29

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)

CodePudding user response:

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");

CodePudding user response:

One way to solve this would be by turning your double number into a string and then you just have to look for the index that contains a "." and replace it with a "," instead.

You could use snprintf(); to do this. All you need is the string where you'll store your double, the maximum length of said string, the format specifier, and the variable that holds your value.

Example code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

#define LENGTH 1000u

int findStringLength(char* userString) {

    int stringLength = 0;
    size_t index = 0;

    while (userString[index] != '\0')
    {
        if (userString[index] != '\0') {
            stringLength  ;
            index  ;
        }

    }

    return stringLength;
}

void dotFinder(char* string) {
    size_t finder;
    size_t nextIndex;

    for (size_t index = findStringLength(string); index > 0; index--) {
        if (string[index] == '.') {
            string[index] = ',';
            break;
        }
    }

    for (size_t index = findStringLength(string); index > 0; index--) {

        nextIndex = index   1;
        finder = string[index];

        if (string[index] == '0' || string[index] == '\0')
        {
            string[index] = '\0';

        }
        else if (string[index] == '.' && string[nextIndex] == '\0')
        {
            string[index] = ',';
            break;
        }
        else if (string[index] != '0')
        {
            break;
        }

    }
}

int main() {
    double doubleVariable = 304.402000;
    char doubleString[LENGTH] = { '\0' };

    snprintf(doubleString, LENGTH, "%lf", doubleVariable);

    dotFinder(doubleString);

    printf(doubleString);

}

The code above outputs 304,402. It Replaces the dot for a comma and removes trailing zeros.

  • Related