Thread: formating numbers

  1. #1
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174

    formating numbers

    I have a long double that represents a dollar amount, say $1,234.56. If I do just
    Code:
    long double number = 1234.56;
    std::cout<<number;
    I get
    Code:
    1234.56
    Is there an IOS flag or something similar I can set to display this number with commas, like this
    Code:
    1,234.56
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Well, there's the somewhat advanced topic of facets and locales that could get you what you want but you're probably better off just using a stringstream to convert the value into a string and then do some string processing on the result to find the '.' character and add the ',' where appropriate.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You are?

    Code:
    #include <iostream>
    #include <locale>
    
    int main()
    {
            std::cout.imbue(std::locale(""));
            std::cout << 12942.51 << std::endl;
    }
    This looks a tiny bit easier to me than doing what you suggest.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    Quote Originally Posted by CornedBee View Post
    You are?

    Code:
    #include <iostream>
    #include <locale>
     
    int main()
    {
            std::cout.imbue(std::locale(""));
            std::cout << 12942.51 << std::endl;
    }
    This looks a tiny bit easier to me than doing what you suggest.
    I was actually looking into that. Works good although I don't yet fully understand all the details.

    I created a function using stringstreams to manually insert the commas. Not too bad, but it came out to about 40 lines. I think I like 2 lines better

    I am going to do some more reading on facets and locales... looks interesting
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's not that complicated. By default, cout is imbued to the C locale, which doesn't do any special formatting. std::locale("") constructs a copy of the system's default locale, which is whatever you specified in the language settings. This locale will format the number according to local conventions. For example, if I ran this on my Windows installation, it would print "12.942,51", because that's the way numbers are formatted in Germany and Austria. (My Linux system is set up with an English locale.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    It was the implementation that I did not quite grasp. I was making it out to be harder than what it is.

    Good bit of information to know.... Thanks!
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Replies: 4
    Last Post: 03-03-2003, 03:52 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM