7

I need that my program shows the output using commas as decimal separator.

According to this link: http://www.java2s.com/Code/Java/Development-Class/Floatingpointwithcommasf.htm you can use in Java something like this:

System.out.printf("Floating-point with commas: %,f\n", 1234567.123);

Is there a flag or something that I can use to have a similar behaviour in C?

Thanks in advance!

2

2 Answers 2

21

If you want, you can set current locale at the beginning of your program:

#include <stdio.h>
#include <locale.h>


int main()
{
    setlocale(LC_NUMERIC, "French_Canada.1252"); // ".OCP" if you want to use system settings
    printf("%f\n", 3.14543);
    return 0;
}
2
  • Similarly, const struct lconv* loc = localeconv(); can be used to get locale specifics. From there you can check loc->decimal_point[0] to see which decimal separator that is currently used.
    – Lundin
    Feb 13, 2017 at 9:42
  • 2
    also you may get current locale with setlocale(LC_ALL, NULL); to restore it afterwards.
    – elephant
    Nov 30, 2017 at 7:49
4

There is no similar functionality in C. You can use sprintf to print into a char array and then replace the dot with a comma manually. I can't think of a better solution, sorry.

EDIT: thanks to Mats Patersson's comment: It seems setting the locale can change this character. Please have a look at the link he posted.

1
  • +1 - I think your answer was better than mine, as locale may be the correct fix. Feb 7, 2013 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.