Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ cout << don't print '0' before decimal point

I didn't find the solution to write decimal number inferior to 1 without the '0' before the decimal point. I want to display numbers at this format : ".1", ".2", etc...

using :

std::cout << std::setw(2) << std::setprecision(1) << std::fixed << number;

always give me formats like "0.1", "0.2", etc...

What do I wrong ? Thanks for your help

like image 514
user3094558 Avatar asked Oct 31 '14 23:10

user3094558


People also ask

Should you put a zero in front of a decimal?

Have you noticed that sometimes this zero is used in decimal values and sometimes it is not? APA Style has a very simple guideline for leading zeros: If a value has the potential to exceed 1.0, use the leading zero. If a value can never exceed 1.0, do not use the leading zero.

How do I restrict decimal points?

Now you can limit the decimal places. Select the number cell and in the Menu, go to Format > Number > More Formats > Custom number format.

How do you change cout precision?

You can set the precision directly on std::cout and use the std::fixed format specifier. double d = 3.14159265358979; cout. precision(17); cout << "Pi: " << fixed << d << endl; You can #include <limits> to get the maximum precision of a float or double.

How do I fix the number of decimal places in C++?

Decimal Places This can be done using the fixed keyword before the setprecision() method. When the fixed keyword is used, the argument in the setprecision() function specifies the number of decimal places to be printed in the output. Here, argument n is the number of decimal places that need to be displayed as output.


1 Answers

You need to convert it to a string and use it for printing. There is no way for a stream to print a floatingpoint without a leading zero, if there is one.

std::string getFloatWithoutLeadingZero(float val)
{
    //converting the number to a string
    //with your specified flags

    std::stringstream ss;
    ss << std::setw(2) << std::setprecision(1);
    ss << std::fixed << val;
    std::string str = ss.str();

    if(val > 0.f && val < 1.f)
    {
        //Checking if we have no leading minus sign

        return str.substr(1, str.size()-1);
    }
    else if(val < 0.f && val > -1.f)
    {
        //Checking if we have a leading minus sign

        return "-" + str.substr(2, str.size()-1);
    }

    //The number simply hasn't a leading zero
    return str;
}

Try it online!

EDIT: Some solution you may like more would be a custom float type. e.g.

class MyFloat
{
public:
    MyFloat(float val = 0) : _val(val)
    {}

    friend std::ostream& operator<<(std::ostream& os, const MyFloat& rhs)
    { os << MyFloat::noLeadingZero(rhs._val, os); }

private:
    static std::string noLeadingZero(float val, std::ostream& os)
    {
        std::stringstream ss;
        ss.copyfmt(os);
        ss << val;
        std::string str = ss.str();

        if(val > 0.f && val < 1.f)
            return str.substr(1, str.size()-1);
        else if(val < 0.f && val > -1.f)
            return "-" + str.substr(2, str.size()-1);

        return str;
    }
    float _val;
};

Try it online!

like image 118
NaCl Avatar answered Oct 05 '22 08:10

NaCl