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

In summary, to achieve an operation in C++ that keeps only 2 decimals and uses floor rounding, you can follow these steps: multiply your floating-point numbers by 100.0, cast them to integers and back to floating-point or use the floor() function to truncate, and then divide by 100.0. To vary the precision, you can use printf with a variable in the first argument, or use the iomanip library with cout.precision().
  • #1
FrostScYthe
80
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
  • #2
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
 
  • #3
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.
 
  • #4
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.
 
  • #5
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...
 
  • #6
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:

1. What is the purpose of setting precision in C++?

The purpose of setting precision in C++ is to control the number of digits displayed after the decimal point when working with floating-point numbers. This can help avoid displaying unnecessary digits and improve the readability of the output.

2. How do I set precision in C++?

The setprecision() function from the iomanip library can be used to set precision in C++. This function takes in an integer value as an argument, representing the number of digits to be displayed after the decimal point. For example, cout << setprecision(2) << 3.14159; would output 3.14.

3. What is floor rounding and how does it work?

Floor rounding is a type of rounding where a number is rounded down to the nearest whole number or decimal place. In the context of setting precision in C++, floor rounding is used to round a floating-point number to a specified number of decimal places by dropping any digits beyond that point. For example, floor(3.14159 * 100) / 100 would result in 3.14 when using 2 decimal places.

4. Can I use floor rounding to round up instead of down?

Yes, you can use the ceil() function from the cmath library to round a number up to the nearest whole number or decimal place. This function works similarly to floor(), but it always rounds up instead of down. For example, ceil(3.14159 * 100) / 100 would result in 3.15 when using 2 decimal places.

5. Are there any other rounding methods available in C++?

Yes, there are several other rounding methods available in C++, such as round() which rounds to the nearest whole number, and trunc() which truncates a number by removing all digits after the decimal point without rounding. These functions can also be used in combination with setprecision() to achieve different rounding results.

Similar threads

Replies
8
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
Replies
4
Views
920
  • Programming and Computer Science
Replies
7
Views
2K
  • Computing and Technology
Replies
4
Views
758
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
8
Views
4K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
2K
  • Classical Physics
Replies
6
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top