Thread: Formatting in c++

  1. #1
    Registered User
    Join Date
    Oct 2008

    Formatting in c++

    is there any way to remove the zero before the decimal point?
    instead of 0.33
    it would print .33

  2. #2
    Registered User
    Join Date
    Oct 2006
    sure, use:
    cout << ".33";
    poorly stated questions give you poor answers.

    however, here is an alternative:
    using namespace std;
    int main()
            float x = 0.33;
            stringstream ss;
            ss << x;
            cout << ss.str().substr(1,ss.str().length()) << endl;
    maybe create a function that takes in a float and returns the string with the leading character (assumed to be a zero) removed?

  3. #3
    Registered User
    Join Date
    Dec 2006
    std::string a("0.33");
    a = a.substr(a.find("."));

  4. #4
    The larch
    Join Date
    May 2006
    What about input such as 10.23 or 1023?

        std::size_t pos = a.find('.');
        if (pos == 1 && a[0] == '0') {
            a = a.substr(pos);
    This might also have locale issues (e.g where , is used for decimal point).

    If C++ accepts ".33" as input for a double then it is a legitimate question whether there is a format flag that lets you output without leading 0 too (I don't know of that).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 05:28 PM
  2. Need a hand formatting numbers.
    By Furious_George in forum C++ Programming
    Replies: 10
    Last Post: 10-15-2003, 10:12 AM
  3. Formatting Standards
    By subdene in forum A Brief History of
    Replies: 20
    Last Post: 11-22-2002, 03:36 PM
  4. Need help with something small - formatting problem
    By wireless in forum C++ Programming
    Replies: 0
    Last Post: 05-28-2002, 05:52 PM
  5. Problem Formatting double numbers.
    By chaps67 in forum C++ Programming
    Replies: 1
    Last Post: 09-19-2001, 09:20 AM
Website Security Test