C/C++ Setting Precision in C++: Floor Rounding to 2 Decimals

  • Thread starter Thread starter FrostScYthe
  • Start date Start date
  • Tags Tags
    C++ Precision
Click For Summary
The discussion revolves around performing calculations in C++ while maintaining a precision of two decimal places and applying floor rounding. The initial operation involves multiplying and dividing floating-point numbers, followed by rounding at each step. A suggested method includes multiplying by 100.0 to preserve two decimal places, casting to integers to truncate excess decimals, and then dividing by 100.0. Another participant raises a concern about losing precision with this approach and suggests calculating with full precision and rounding only at the end. The conversation shifts to formatting output, where one user seeks to adjust the precision of printed floating-point numbers using `printf`. While they initially prefer `printf` for its formatting, alternatives using the C++ i/o library, such as `cout.precision()`, are also mentioned, emphasizing that the first argument in `printf` can be dynamic rather than a constant string.
FrostScYthe
Messages
80
Reaction score
0
Hey.. I'm having trouble achieving this in C++...

I'm trying to do an opperation but keeping only 2 decimals, and floor rounding.

((3.52 * 4.32)/3.26) + 2.34

So I have 3.52 * 4.32 = 15.2064, but then I just round to 15.20

then 15.20/3.26 = 4.662576687116, but then I just round to 4.66

... and so on...

anyone know how to achive this in C++, I've thought of the floor command but how do I see the precision to only 2 decimals?
 
Technology news on Phys.org
At each intermediate step, you'll need to:

1) Multiply your floating-point numbers by 100.0 (to preserve two decimal places).

2) Cast them to first to integers and then back to floating-point (which will truncate the rest of the decimal places). Alternatively, you can use the floor() function to truncate.

3) Divide the resulting floating-point number by 100.0.

- Warren
 
FrostScYthe said:
Hey.. I'm having trouble achieving this in C++...

I'm trying to do an opperation but keeping only 2 decimals, and floor rounding.

((3.52 * 4.32)/3.26) + 2.34

So I have 3.52 * 4.32 = 15.2064, but then I just round to 15.20

then 15.20/3.26 = 4.662576687116, but then I just round to 4.66

... and so on...

anyone know how to achive this in C++, I've thought of the floor command but how do I see the precision to only 2 decimals?
Why would you ever want to do that anyway? You'll just lose precision. It makes more sense to calculate with all the digits you have and only round at the end.
 
Alright... I have the rounding working now, Thanks so much =).

I'm printing the matrix giving a precision of 5 decimal places... I use

int j, k;
for (j = 0; j < n; j++) {
for (k = 0; k < n + 1; k++) {
printf (" %10.5f ", a[k][j]);
}
printf("\n");
}
printf("\n");

however I want to vary the " %10.5f " to any precision I input... anyone know how to do this... I know there's a way with iomanip.. but I want to use printf because it does it nicer.
 
I would prefer to advise using the C++ i/o library. But, if you insist...

The first argument to printf doesn't have to be a string constant...
 
For more decimal places you could also try putting this in your code

cout.precision(16);

You need not use 16, but that(16 digits) is about the limit of you precision, if you are using type double.
 
Last edited:
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
Replies
4
Views
2K
Replies
4
Views
2K
Replies
5
Views
3K
  • · Replies 26 ·
Replies
26
Views
7K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K