Round number to nearest “nth” based on first non zero

瘦欲@ 提交于 2019-12-10 16:54:31

问题


I want to round a Double to the nearest non zero number that follows the decimal.

For example:

x = 0.002341
rounded = 0.002

x = 0.000048123
rounded = 0.00005

For cases where the base number is > 0, it should perform as such

x = 1.000234
rounded = 1.0002

I know I can use Double(round(1000*x)/1000) if I know the number of digits, but I want it to work for any number. Is there a swift function that does this?


回答1:


You can have a little fun with logarithms to solve this:

func roundFirst(x:Double) -> Double {
    if x == 0 {
        return x;
    }
    let mul : Double = pow(10, floor(log10(abs(x))))
    return round(x/mul)*mul
}

The non-fractional part of log10(abs(x)) gives you a positive or negative power of ten of the inverse of the number which you use as your multiplier. floor drops the fraction, and pow(10,...) gives you a multiplier to use in your rounding trick.

I tried this in the playground with a few numbers. Here is what I've got:

println(roundFirst(0.002341))     // 0.002
println(roundFirst(0.000048123))  // 5e-05
println(roundFirst(0.0))          // 0.0
println(roundFirst(2.6))          // 3.0
println(roundFirst(123.0))        // 100
println(roundFirst(-0.002341))    // -0.002
println(roundFirst(-0.000048123)) // -5e-05
println(roundFirst(-2.6))         // -3.0
println(roundFirst(-123.0))       // -100



回答2:


You haven't specified what happened when x = 1.002341 for examples. Base on your limited cases you can use this:

func myRound (var number : Double) -> Double {
    if number == 0 {
        return number
    }

    let integerPart = floor(number)
    var base = 1.0

    number -= integerPart
    while abs(number) < 1 {
        number *= 10.0
        base *= 10.0
    }

    return integerPart + round(number) / base
}


来源:https://stackoverflow.com/questions/32193876/round-number-to-nearest-nth-based-on-first-non-zero

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!