0

I am still quite new in the C++ business. I want to output a float number with only one decimal number. At first I thought it would work with Modulo (%), but I quickly discarded this. Then I wanted to solve it calculated, but I couldn't solve this. There is also a clue. I can only use <iostream> and <fstream>. But now I came across the function setprecision().

My output code is. cout<<fixed<<setprecision(2)<<variable<<endl; However, it outputs e.g. 2.99. I need 2.9 though. cout<<fixed<<setprecision(1)<<variable<<endl; Outputs 3.0, though.

Please help me, I will be infinitely grateful. :)

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
T K
  • 19
  • 4
  • 3
    So, you're claiming that the same line of code somehow prints two values? Your question is unclear. In any case, if `setprecision(2)` results in two decimal points of precision don't you think it makes sense to try to see what `setprecision(1)` does? Before posting their first question on stackoverflow.com, everyone should take the [tour], read the [help], understand all the requirements for a [mre] and [ask] questions here. Not doing any of this results in a poor quality question almost every time. It then gets downvoted, closed, and then deleted. – Sam Varshavchik Jan 01 '22 at 16:53
  • I only use one line of this in my code. If I use setprecision(2) my output is 2.99. If I use setprecision(1) my output is 3. Sorry, I did a spelling mistake. – T K Jan 01 '22 at 16:56
  • For me, if I use `setprecision(1)` my output is `3.0`, not `3`. That's because `2.99` rounds to `3.0`. It's printing to the console with one decimal digit of precision. If you don't want the rounded value, what do you want? – Nathan Pierson Jan 01 '22 at 16:59
  • @SamVarshavchik I assumed it was a typo in the question. https://stackoverflow.com/questions/5907031/printing-the-correct-number-of-decimal-points-with-cout – DS_London Jan 01 '22 at 17:00
  • `2.99` rounded to the closest value with 1 digit of precision is `3.0`. If you want to truncate a value to a certain number of digits it's probably possible to slap something together using some math and `trunc()`, however the results will not be very stable [because, as we all know, floating point math is broken](https://stackoverflow.com/questions/588004/). – Sam Varshavchik Jan 01 '22 at 17:02
  • 2
    You want to round downwards? Try std::fesetround(FE_DOWNWARD); or std::fesetround(FE_TOWARDZERO); or (for positive numbers) cout<< fixed< – Sebastian Jan 01 '22 at 17:02
  • 2
    It's unusual to want to display 2.99 as 2.9; the standard library, without some funky intervention, will display it as 3.0. If you want 2.99, probably the easiest way to do it is to convert the value into a string and do the truncation yourself. – Pete Becker Jan 01 '22 at 17:04
  • I have the task, to write a program for calculating the mark of a bachelors degree. I do a division with 2 floats. If the result of the division is e.g. 2.9785, I need a 2.9. if I write setprecision(2)2.99. with setprecision(1) I get 3. but I need 2.9 No round – T K Jan 01 '22 at 17:06
  • @Sebastian makes a good point: since the conversion from floating-point to text on output rounds the value to the desired number of digits, you just need to undo the rounding in advance by subtracting 0.5. – Pete Becker Jan 01 '22 at 17:07
  • 1
    if you want 2.99 to become 2.9, you want to truncate to one decimal place. That can be done by multiplying by 10, drop decimal part, divide by 10. You'll still have an issue with floating point imprecision but it should be small enough to not matter when using `setprecision`. – NathanOliver Jan 01 '22 at 17:08
  • @Sebastian solved the Problem. Thank you. – T K Jan 01 '22 at 17:11
  • @Sebastian made an answer from your comment. – JohnFilleau Jan 01 '22 at 17:18

3 Answers3

2

Illustrating the floating-point environment solution from Sebastian in the comments

#include <fenv.h>
#include <iostream>
#include <iomanip>

int main() {
    if (fesetround(FE_TOWARDZERO)) {
        // need to check for non-zero error condition
        std::cerr << "Unable to set correct rounding direction" << std::endl;
        return 1;
    }

    float value = 2.99f;

    std::cout << std::setprecision(2) << value << std::endl;
}

This prints

2.9

Note that this will also affect other floating point calculations, which may cause unexpected results.

I don't know how widespread support is for these different rounding directions on different target architectures, so if you want extreme portability you'll need to provide a back up solution if fesetround returns a non-zero value.

godbolt

JohnFilleau
  • 4,045
  • 1
  • 15
  • 22
1

Here's one way:

#include <math.h>
#include <iostream>

int main() {

    auto f = 2.9999;

    std::cout << trunc(f * 10) / 10 << '\n';

}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
1

Sebastian solved it in the comments.

cout<< fixed<<setprecision(1)<<variable-.05<<endl;

Thank you all for contributing. I'll try to improve my

T K
  • 19
  • 4