Reputation: 6316
cout<<5*1.0<<endl;
cout<<(float)5<<endl;
In both cases I got 5 as an answer and not 5.0. Please help...
Upvotes: 4
Views: 309
Reputation: 173014
You need to specify the format and precision for floatingPoint output, such as:
std::cout << std::fixed << std::setprecision(1) << 5*1.0;
From [The.C++.Programming.Language.Special.Edition] 21.4.3 FloatingPoint Output [io.out.float]:
Floatingpoint output is controlled by a format and a precision:
– The general format lets the implementation choose a format that presents a value in the style that best preserves the value in the space available. The precision specifies the maximum number of digits. It corresponds to printf()’s %g (§21.8).
– The scientific format presents a value with one digit before a decimal point and an exponent. The precision specifies the maximum number of digits after the decimal point. It corresponds to printf()’s %e .
– The fixed format presents a value as an integer part followed by a decimal point and a fractional part. The precision specifies the maximum number of digits after the decimal point. It corresponds to printf()’s %f .
Upvotes: 4