Thread: Comma instead of period in Double output

  1. #1
    Registered User
    Join Date
    Oct 2010

    Comma instead of period in Double output

    Is there an easy way to change the output from a double char from the usual period to a comma with a simply command? like 125.15 -> 125,15

    Or do you have to make a loop where you replace every . with , ?

  2. #2
    Registered User
    Join Date
    Sep 2007
    Use the setlocale() function:
    #include <locale.h>
    setlocale(LC_ALL, ""); /* sets all locale information (collation, character classification, etc) */
    setlocale(LC_NUMERIC, ""); /* affects just the number formatting */
    By using "" (the empty string) you tell setlocale() to use system defaults, which at least on Unix-like systems will be set through environment variables. So if you set $LANG (or $LC_NUMERIC) to, say fr_FR, and use printf("%f\n", 5.5), it should output 5,5.

    You can also pass a string (which is system-specific) as the second argument to force a particular language. This is less desirable because it doesn't allow the user to choose.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes and objects
    By GoldenBoy in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2010, 07:28 AM
  2. Travel Expenses-Weird Result
    By taj777 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2010, 02:23 PM
  3. Functions, have errors...NEED HELP FAST
    By alkamenes in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2009, 03:00 PM
  4. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM