Rounding a double number up to the tenths place [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
round() for float in C++
Ok suppose I had the number 8.47434. I want to round it up to 8.5 and to 1 decimal place. How would I go about doing this in C++

Multiply by 10, round and divide by 10 again.
Example:
round(10 * 8.47434f) / 10;
Edit:
OK, I just found out that round() is not always present in math.h.
The above works in gcc and icl (with Microsoft's libraries), but not in tcc.
floor(), however, is part of the standard libraries. So to round, we can add 0.5 and use floor().
Example:
floor(10 * 8.47434f + 0.5f) / 10;

std::floor(d)+std::floor((d-std::floor(d))*10.0+0.5)/10.0
Doing it this way won't lose precision, as opposed to the other answers which multiply the original double by 10. (d would be your number by the way)
Be forewarned though: floating point numbers can't represent anything. With doubles: 1.35 will round to 1.3999999999999999; 123.34 will be represented as 123.34999999999999 in the first place causing it to round down to 123.3 instead of the intended 123.4; etc

You could multiple by 10 add 0.5 cast to an int and divide by ten.
if the number is negative subtract 0.5 or multiply and divide by negative ten.

Related

Double Decimal Rounding in C++ [duplicate]

I'm trying to allow my program to round a number up and down respectively.
For example, if the number is 3.6, my program is suppose to round up the nearest number which is 4 and if the number is 3.4, it will be rounded down to 3.
I tried using the ceil library to get the average of 3 items.
results = ceil((marks1 + marks2 + marks3)/3)
However, the ceil only rounds the number down but does not roll the number up.
There's 1 algorithm i stumbled upon
var roundedVal = Math.round(origVal*20)/20;
but i still can't figure a formula for some problem.
std::ceil
rounds up to the nearest integer
std::floor
rounds down to the nearest integer
std::round
performs the behavior you expect
please give a use case with numbers if this does not provide you with what you need!
You don't need a function to round in C or C++. You can just use a simple trick. Add 0.5 and then cast to an integer. That's probably all round does anyway.
double d = 3.1415;
double d2 = 4.7;
int i1 = (int)(d + 0.5);
int i2 = (int)(d2 + 0.5);
i1 is 3, and i2 is 5. You can verify it yourself.
The function you need is called round, believe it or not.
ceil rounds UP, btw. That is, to the closest larger integer. floor rounds down.
std::round may be the one you're looking for. However, bear in mind that it returns a float. You may want to try lround or llround to get a result in long or long long (C++ 11).
http://en.cppreference.com/w/cpp/numeric/math/round
In c++, by including cmath library we can use use various functions which rounds off the value both up or down.
std::trunc
This simply truncates the decimal part, thas is, the digits after the decimal point no matter what the decimal is.
std::ceil
This is used to round up to the closest integer value.
std::floor
This is used to round down to the closest integer value.
std::round
This will round to the nearest integer value whichever is the closest, that is, it can be round up or round down.

Strange rounding behavior [duplicate]

This question already has answers here:
C++ floating point precision [duplicate]
(5 answers)
Closed 5 years ago.
I'm trying to round doubles to a specific precision, however the following functions give me different results:
Version 1:
static double RoundPrecision(double& val)
{
val = floor(val * 1000 + 0.5) * 0.001;
return val;
}
Version 2:
static double RoundPrecision(double& val)
{
val = floor(val * 1000 + 0.5) / 1000;
return val;
}
Example output when rounding the number 300.9:
Version 1: 300.90000000000003
Version 2: 300.89999999999998
Both versions sometimes give the same result, but for specific inputs the results differ. I have to have consistent behavior when equating numbers to other variables in the program.
EDIT:
I am aware of the problems with floating point precision, which is exactly what I'm trying to avoid here by rounding. I need a consistent way to round to 3 decimal point precision.
Computers are limited in their precision. You either accept the available precision in double or float, or you seek other libraries that give you sufficient precision, but with worse performance. Realistically, you cannot achieve exact real-numbers on computers, and you won't even need it. double gives you 10^-16 relative precision, then try long double. If that isn't enough, then seek external libraries that would do that for you. Here's an example of an arbitrary precision library.
From the comments I see that you need 3 decimal places precision. If you read this number up to 3 decimal points, then both results are the same. I think the reasons you're confused is that you don't know how to compare floats. Here's how you do it.

Rounding up and down a number C++

I'm trying to allow my program to round a number up and down respectively.
For example, if the number is 3.6, my program is suppose to round up the nearest number which is 4 and if the number is 3.4, it will be rounded down to 3.
I tried using the ceil library to get the average of 3 items.
results = ceil((marks1 + marks2 + marks3)/3)
However, the ceil only rounds the number down but does not roll the number up.
There's 1 algorithm i stumbled upon
var roundedVal = Math.round(origVal*20)/20;
but i still can't figure a formula for some problem.
std::ceil
rounds up to the nearest integer
std::floor
rounds down to the nearest integer
std::round
performs the behavior you expect
please give a use case with numbers if this does not provide you with what you need!
You don't need a function to round in C or C++. You can just use a simple trick. Add 0.5 and then cast to an integer. That's probably all round does anyway.
double d = 3.1415;
double d2 = 4.7;
int i1 = (int)(d + 0.5);
int i2 = (int)(d2 + 0.5);
i1 is 3, and i2 is 5. You can verify it yourself.
The function you need is called round, believe it or not.
ceil rounds UP, btw. That is, to the closest larger integer. floor rounds down.
std::round may be the one you're looking for. However, bear in mind that it returns a float. You may want to try lround or llround to get a result in long or long long (C++ 11).
http://en.cppreference.com/w/cpp/numeric/math/round
In c++, by including cmath library we can use use various functions which rounds off the value both up or down.
std::trunc
This simply truncates the decimal part, thas is, the digits after the decimal point no matter what the decimal is.
std::ceil
This is used to round up to the closest integer value.
std::floor
This is used to round down to the closest integer value.
std::round
This will round to the nearest integer value whichever is the closest, that is, it can be round up or round down.

C++ program is rounding by default [duplicate]

This question already has answers here:
Division not outputting correct answer c++
(2 answers)
Closed 7 years ago.
I'm currently working on a C++ program where I have a bankAccount class and I need to calculate interest. Problem is, my function is rounding off my numbers to the whole number, even though I'm using a float as my variable type. So if I had code like this:
float BankAccount::CalculateInterest(int Time)
{
float thing;
thing = (967 / 365);
return thing;
}
thing ends up equaling 2.00000000, when it should equal 2.649315068.
Any ideas? I had my interest equation in there before, and the result was always 1.00000000 no matter what, so I put this in as a test and I saw that it's rounding to the "floor".
Thanks for your help!
It's not rounding.
You are dividing two integers and the result will be an integer.
You can remedy this by simply adding ".0" to both numbers, or explicitly defining them as floats.
967 and 365 are integers, so integer division is used and the remainder discarded. Make one a floating point number and floating point division will be done: 967 / 365.0.
Alternatively:
float thing = 967; // integer converted to float during assignment
thing /= 365; // division of float and converted integer

Reading string in to float produces incorrect accuracy [duplicate]

This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Closed 8 years ago.
Now I understand floats are less accurate than double, but does this explain when I have the std::string:
"7.6317"
and I do:
float x = atof(myString.c_str());
getting 7.63170004 is expected? Is there any way I can tell the assignment of x to only read the first 4 decimal places? Or is this because of the way the float representation stores the number 7.6317?
Yes. It is expected. It is so-called floating point error.
Some floating point literals do not have an accurate representation in the computer, even if -- in decimal notation -- the number seems harmless. This is because the computer uses 2 as a base. So even if a number might have a finite representation in base 10, it might not have on in base 2.
you can do it like:
float x = floorf(val * 10000) / 10000;
i think it should work! see See