1

有一个双精度数和所需的舍入数,我如何得到下一个bigger/smaller rounded number

样本

N = 10.476
Prec = 0.25
Smaller = 10.25
Bigger = 10.50

N = 0.837
Prec = 0.005
Smaller = 0.835
Bigger = 0.840
4

1 回答 1

1
def round_smaller(n, prec)
  (n / prec).floor * prec
end

def round_bigger(n, prec)
  (n / prec).ceil * prec
end
于 2013-07-18T04:59:59.143 回答