23

I'm new to C++. I have a double variable double a=0.1239857 and I want to limit variable a from decimal point two digits. So a will be 0.12. I know C++ have functions that return largest or smallest integer that is greater or lower than a like ceil or floor.

Is there a function that implements digit limitation of floating-point variable? Or How can I change precision of the a variable?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
yalcin
  • 541
  • 4
  • 12
  • 23
  • 1
    Using std::fixed as shown in this answer would result in the required format http://stackoverflow.com/questions/5907031/printing-the-correct-number-of-decimal-points-with-cout – Rajith Gun Hewage Jan 05 '16 at 13:01

9 Answers9

25

Are you actually trying to round the number, or just change its displayed precision?

For the former (truncating the extra digits):

double scale = 0.01;  // i.e. round to nearest one-hundreth
value = (int)(value / scale) * scale;

or (rounding up/down as appropriate, per jheriko's answer)

double scale = 0.01;  // i.e. round to nearest one-hundreth
value = floor(value / scale + 0.5) * scale;

For the latter:

cout << setprecision(2) << value;

where the parameter to setprecision() is the maximum number of digits to show after the decimal point.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • 2
    Note that when you use setprecision on a stream like you show, it will round also. – Brian Neal Apr 28 '09 at 14:02
  • Brian - please elaborate. Sure, it'll _show_ the value rounded up or down to the appropriate number of places, but it won't change the value actually stored in the variable. – Alnitak Apr 28 '09 at 14:42
  • The value will be rounded to the appropriate precision upon display to cout. Of course it has no effect on the value in the variable. – Brian Neal Apr 28 '09 at 15:56
  • I was trying to say it does both: setprecision shows the value to the desired precision, *and* rounds while doing so. – Brian Neal Apr 28 '09 at 15:58
  • You need to use "fixed" as well. `cout << fixed << setprecision(2) << value;` – amit kumar Jan 08 '17 at 14:42
9

This will result in two digits after the decimal place.

a = floor(a * 100.0) / 100.0;
bradtgmurray
  • 13,683
  • 10
  • 38
  • 36
  • Although not asked by OP: Note that when printing `a` it is not guaranteed to show only 2 digits. It will have rounding errors. – AbdealiLoKo Jan 24 '17 at 05:00
4

If you just want to output the value, you can do something like

printf("%.3f", a); // Output value with 3 digits after comma

If you want to convert the value itself, you can do:

a = (int)(a * 1000) / 1000.0f;

Note that both do no rounding, they just truncate the value.

schnaader
  • 49,103
  • 10
  • 104
  • 136
3

What do you mean by you want to limit the variable ? The value or its formatting. For the value, you can use floor + division. Something like:

double a = 0.12123
double b;

b = floor(a * 100) / 100
David Cournapeau
  • 78,318
  • 8
  • 63
  • 70
1

you could also do something like this:

//This code will ask the user for an input, set the decimal precision to the hundredths place,  and add 4.63 to the inputted variable

int banana;
cin >> banana;
cout << setprecision(2) << fixed << banana + 4.63; 
Joe Schmoe
  • 11
  • 1
1

Use a ios_base::precision for formatting i/o.

dirkgently
  • 108,024
  • 16
  • 131
  • 187
1

You can set the precision on a stream, e.g.

double d = 3.14579;
cout.precision(2);
cout << d << endl;

// Or use a manipulator

#include <iomanip>
cout << setprecision(2) << d << endl;

Note that when you send a double or float to a stream like this, it will automatically round for you (which can trip you up sometimes if you aren't aware of this).

Brian Neal
  • 31,821
  • 7
  • 55
  • 59
1

An actual rounding solution would be x = floor(100*x + 0.5) / 100; assuming the value to be rounded is in a variable "x".

The x = floor(100*x) / 100; recommended by others here will actually truncate the number to 2dp instead.

jheriko
  • 3,043
  • 1
  • 21
  • 28
0

You can write your own function as follows, it can handle rounding errors for decimals as well.

double RoundForGivenPrecision(const double dNumber, int iDecimalPlaces)
{
long long multiplier = (long long)pow(10, iDecimalPlaces);
long double value = dNumber < 0 ? (long long)((nextafter(dNumber, -DBL_MAX)*multiplier)-0.5) : (long long)((nextafter(dNumber,DBL_MAX)*multiplier)+0.5);
return value / multiplier;
}