Hi,
I'm a Java programmer, so I got rather confused, when google didn't give me any usable answers for rounding function.
I need a function, that would round a double value to 3 digts of fractional part.
Thanx in advance.
Printable View
Hi,
I'm a Java programmer, so I got rather confused, when google didn't give me any usable answers for rounding function.
I need a function, that would round a double value to 3 digts of fractional part.
Thanx in advance.
looking for something like this ?
OutputCode:double d = 12.3446;
d = floor(d * 1000.0 + 0.5 ) / 1000.0;
Code:12.345
Thanx :)
Sorry for BUMPing the topic, but I didn't actually understand, HOW that function works...Again google didn't help me :\
How can I round a number to 2 digts?
Just follow it one step at a time with the number. First it is 12.3446, then you multiply by 1000 so it becomes 12344.6, then you add 0.5 so it is 12345.1, then you call floor, which removes the decimal part to make it 12345.0, then you divide by 1000 to make it 12.345.
>> How can I round a number to 2 digts?
You should be able to figure it out once you understand how the original code works.
The problem is, that I don't know, what the number is...It's a result of a calculation and it might be whatever...Quote:
Originally Posted by Daved
It doesn't matter what the original number is... follow the steps with any positive number instead of 12.3446, it will still work.
It works because it uses multiplication by a power of ten to get the appropriate digit to be the first digit to the right of the decimal. Then it adds 0.5 and cuts off that decimal using floor, which is the same as rounding to the nearest integer (think about why). Then it uses division by the same power of ten to get the decimal point back to where it was originally.
Here this should do the trick for you.
I'm not really qualified to be helping people because I just started a few weeks ago but I think I got this one.Code:double myRounder()
{
int decimalPlaces = 3; //the number of decimal places to round to.
double roundableInput = 12.3446; //The number you want to round
double roundedOutput = 0; //The rounded version of our number once the function runs
roundedOutput = floor(roundableInput * pow(10,decimalPlaces) + 0.5) / pow(10,decimalPlaces);
return roundedOutput;
};
I appreciate Daved's effort to educate you, dogbert, and instead of spelling it out for you, will try to make his point even clearer.
If you have 1234.56789 and you want to round to 1 decimal digit you would want to perform the aformentioned algorithm with 12345.6789 and to yield that number you just multiply 1234.56789 times 10. If you want 2 digits rounded, you would somehow transform 1234.56789 into 123456.789 by multiplying times 100. Et cetra. As should start becoming obvious, you multiple with as many zeros (right of the one) to which you wish to round. Determining how that may be accomplished should be your final quest, and it's pretty straightforward.
Can't use just use a cast instead of floor?