-
Rounding
How do I do if I want to round numbers to the closest number with X digits to the left and thwen just zeroes. I give an example to give a better idea:
69486 rounded with 2 digits is 69000
2623 is 2600
93 is 93
2000 is 2000
What´s the easist way to do this? Is there any special API for it?
-
Are we talking about integers or floating point?
As far as I'm aware, there is no library or API function to do this, so you'll have to do it "by hand", and it's relatively easy to do for integers, a bit more comlicated for floating point.
For integer, you essentially need to find out the number of digits in the number, and then divide and multiply the number so that you end up with the number you want. You should also consider that 69586 is 70000 when you round it, so you need to add a bit before you chop off the number.
In this case, the code would look like this:
Code:
int x = 69586;
x += 500;
x /= 1000;
x *= 1000;
This is of course not generic, but it's not terribly hard to write a function that does it for "any number with any number of digits".
--
Mats
-