I'm trying to think of a function in C that would satisfy the following conditions:
- It accepts an integer greater than 0 as an argument;
- It rounds that integer up to the nearest value so that only the first digit is not a zero
For example:
53 comes out as 60..
197 comes out as 200..
4937 comes out as 5000..
Is there a way to do this so that the requirement is satisfied regardless of the number of trailing zeroes?
For example, I understand how I could do it in any individual case. divide 53 by 10 then ceil(), multiply by 10,
but I would like one that can handle any value.
Opinions? Ideas?
I am not sure if you want round or ceil. But the behavior you show in the question suggests ceil. So I included that.
EDIT
Changed the code to remove ceil and other extra operations.
EDIT 2
Fixed for multiples of 10.
No loops.
Avoid string conversions and loops:
Try taking the first character of the input number, add 1, then append zeros.
In VB, but hopefully you get the idea.
Some expert advice you get here... To round a number i up to nearest 10:
Similarly for 100s, 1000s, etc.