我有一个像 30.6355 这样代表金钱的数值,如何四舍五入到小数点后两位?

发布于 2024-10-01 10:42:13 字数 45 浏览 2 评论 0原文

我有一个像 30.6355 这样代表金钱的数值,如何四舍五入到小数点后两位?

I have a numeric value like 30.6355 that represents money, how to round to 2 decimal places?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

审判长 2024-10-08 10:42:13

处理货币时不应使用doublefloat 类型:它们的小数位数过多且偶尔会出现舍入错误。金钱可能会从这些漏洞中掉出来,并且在错误发生后很难追查到错误。

处理金钱时,请使用固定的小数类型。在 Ruby(和 Java)中,使用 BigDecimal。

You should not use double or float types when dealing with currency: they have both too many decimal places and occasional rounding errors. Money can fall through those holes and it'll be tough to track down the errors after it happens.

When dealing with money, use a fixed decimal type. In Ruby (and Java), use BigDecimal.

多彩岁月 2024-10-08 10:42:13

Ruby 1.8:

class Numeric
    def round_to( places )
        power = 10.0**places
        (self * power).round / power
    end
end

(30.6355).round_to(2)

Ruby 1.9:

(30.6355).round(2)

在 1.9 中,round< /a> 可以四舍五入到指定的位数。

Ruby 1.8:

class Numeric
    def round_to( places )
        power = 10.0**places
        (self * power).round / power
    end
end

(30.6355).round_to(2)

Ruby 1.9:

(30.6355).round(2)

In 1.9, round can round to a specified number of digits.

雨轻弹 2024-10-08 10:42:13

这将针对一些有用的案例 - 写得不好,但它有效!请随意编辑。

def round(numberString)
numberString = numberString.to_s
decimalLocation = numberString.index(".")
numbersAfterDecimal = numberString.slice(decimalLocation+1,numberString.length-1)
numbersBeforeAndIncludingDeciaml = numberString.slice(0,decimalLocation+1)

if numbersAfterDecimal.length <= 2
    return numberString.to_f
end

thingArray = numberString.split("")
thingArray.pop

prior = numbersAfterDecimal[-1].to_i
idx = numbersAfterDecimal.length-2

thingArray.reverse_each do |numStr|
    if prior >= 5
        numbersAfterDecimal[idx] = (numStr.to_i + 1).to_s unless (idx == 1 && numStr.to_i == 9)
        prior = (numStr.to_i + 1)
    else
        prior = numStr.to_i
    end
    break if (idx == 1)
    idx -= 1
end

resp = numbersBeforeAndIncludingDeciaml + numbersAfterDecimal[0..1]
resp.to_f
end

round(18.00) == 18.0

round(18.99) == 18.99

round(17.9555555555) == 17.96

round(17.944444444445) = = 17.95

round(15.545) == 15.55

round(15.55) == 15.55

round(15.555) == 15.56

圆(1.18) == 1.18

圆(1.189) == 1.19

This will round for some useful cases - not well written but it works! Feel free to edit.

def round(numberString)
numberString = numberString.to_s
decimalLocation = numberString.index(".")
numbersAfterDecimal = numberString.slice(decimalLocation+1,numberString.length-1)
numbersBeforeAndIncludingDeciaml = numberString.slice(0,decimalLocation+1)

if numbersAfterDecimal.length <= 2
    return numberString.to_f
end

thingArray = numberString.split("")
thingArray.pop

prior = numbersAfterDecimal[-1].to_i
idx = numbersAfterDecimal.length-2

thingArray.reverse_each do |numStr|
    if prior >= 5
        numbersAfterDecimal[idx] = (numStr.to_i + 1).to_s unless (idx == 1 && numStr.to_i == 9)
        prior = (numStr.to_i + 1)
    else
        prior = numStr.to_i
    end
    break if (idx == 1)
    idx -= 1
end

resp = numbersBeforeAndIncludingDeciaml + numbersAfterDecimal[0..1]
resp.to_f
end

round(18.00) == 18.0

round(18.99) == 18.99

round(17.9555555555) == 17.96

round(17.944444444445) == 17.95

round(15.545) == 15.55

round(15.55) == 15.55

round(15.555) == 15.56

round(1.18) == 1.18

round(1.189) == 1.19

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文