Number scaling

How to increase the number to ten, hundreds of thousands, etc ...

Ref.

num = 11 round up to 20
num = 15 round up to 20
num = 115 round up to 200
num = 4334 round up to 5000
+3
source share
4 answers

I think this formula can work? If you have more examples to show.

power = floor(log10(n))
result = (floor(n/(10^power)) + 1) * 10^power
+2
source

Convert the number to decimal (i.e. 11 goes to 1.1, 115 goes to 1.15), then take the ceiling of the number, and then multiply it back. Example:

public static int roundByScale(int toRound) {
    int scale = (int)Math.pow(10.0, Math.floor(Math.log10(toRound)));
    double dec = toRound / scale;
    int roundDec = (int)Math.ceil(dec);
    return roundDec * scale;
}

In this case, if you enter 15, it will be divided by 10 to become 1.5, then rounded to 2, then the method will return 2 * 10, which is 20.

+1
source
import math

exp = math.log10(num)
exp = math.floor(exp)
out = math.ceil(num/10**exp)
out = out * 10**exp
+1
source
public static int ceilingHighestPlaceValue(int toCeil) 
{
    int placeValue = Math.Pow(10,toCeil.ToString().Length()-1);
    double temp = toCeil / placeValue;
    return= ceil(temp) * placeValue; 
}
0
source

Source: https://habr.com/ru/post/1756263/


All Articles