Thread: converting floating point number to a currency display

  1. #1
    Registered User
    Join Date
    Jun 2010

    converting floating point number to a currency display

    So im trying to generate a program that will take a floating point number and have the output display it as a currency, example:

    input 34534.3675
    output $34,534.37

    input 99.1
    output $99.10

    input 2030
    output $2,030.00

    Any help on what algorithms I should have in my function aruguments ?

  2. #2
    Registered User
    Join Date
    Jun 2009
    You can do all that using simple printf format strings

  3. #3
    Registered User
    Join Date
    Sep 2008
    Toronto, Canada
    Well, no. Comma separation every 3 digits is not built into standard formatting.

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    You can do all that using simple printf format strings
    However the flags are an extension of the standard and documentation is difficult to come by.

    In order to use the thousands separator flag, which is a single-quote ( ' ) you have to set the locale. C has its own default locale but it contains no thousands separator. The easiest way to go about setting the locale is to let the function figure out which one to use:
    #include <locale.h>
    setlocale(LC_ALL, "");
    After that the thousands separator will work (and it will be correctly based on the user's language settings, too!).

    As for the decimal, that's done with the "." modifier - that's part of the standard and is pretty well documented if you aren't already familiar with it.
    And the dollar sign can just be before the formatting part for the number. If you want to be agnostic you can make a call to localeconv() and find out what the local currency sign is.

    So, the general purpose printing function is this:
    #include <locale.h>
    #include <stdio.h>
    struct lconv *locale;
    locale = localeconv();
    float value = 1234.567;
    printf("%s%'.2f\n", locale->currency_symbol, value);
    I'm feeling very generous today, I guess.
    Last edited by bernt; 06-28-2010 at 03:12 PM. Reason: Left out a semicolon
    Consider this post signed

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    CA, USA
    You'll want to use locales for this. Look into the functions in <local.h>

    EDIT: Nevermind, nice post above

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. HELP! Changing the base of a floating point number..
    By yigster in forum C Programming
    Replies: 6
    Last Post: 03-27-2008, 05:36 AM
  3. floating point function
    By Sal79 in forum C++ Programming
    Replies: 17
    Last Post: 04-17-2007, 06:49 PM
  4. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
Website Security Test