ruby 环境中的 java.bigDecimal 除法

发布于 2024-08-31 09:47:06 字数 510 浏览 6 评论 0原文

我在 Ruby 中正确编写了包含 java 类的脚本,

require 'java'
include_class 'java.math.BigDecimal'
include_class 'java.math.RoundingMode'

在脚本中我需要除以 2 java.bigDecimal,

 one = BigDecimal.new("1")
 number1 = BigDecimal.new("3")
 number1 = one.divide(number1,RoundingMode.new(HALF_EVEN))

因为我在这个 IDE 中没有智能感知,我不确定语法是否正确,运行时错误是:

未初始化常量::HALF_EVEN

  1. 我是否以正确的方式在 ruby​​ 脚本中组合 java 对象?
  2. 我应该如何在 ruby​​ env 中划分两个 java.bigDecimal 对象?

I right script in Ruby that include java classes

require 'java'
include_class 'java.math.BigDecimal'
include_class 'java.math.RoundingMode'

during the script I need to divide 2 java.bigDecimal

 one = BigDecimal.new("1")
 number1 = BigDecimal.new("3")
 number1 = one.divide(number1,RoundingMode.new(HALF_EVEN))

since I don't have intellisense in this IDE I'm not sure the syntax is right and the runtime error is:

uninitialized constant::HALF_EVEN

  1. do I combine java object in the ruby scrpit in the right way?
  2. how should I divide two java.bigDecimal object in ruby env?

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

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

发布评论

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

评论(2

ゞ花落谁相伴 2024-09-07 09:47:06

尝试

number1 = one.divide(number1, RoundingMode::Half_EVEN)

Try

number1 = one.divide(number1, RoundingMode::Half_EVEN)
同尘 2024-09-07 09:47:06

在 Java 中它是 RoundingMode.HALF_EVEN;它是 Ruby 中的 RoundingMode::HALF_EVEN。您还可以使用 int 常量重载(即 BigDecimal::ROUND_HALF_EVEN),但是 enum 重载绝对是正确的选择。

您可以使用 divide(BigDecimal divisor, int scale, RoundingMode mode) 重载。

下面是一个 Java 片段:

    BigDecimal one = BigDecimal.ONE;
    BigDecimal three = BigDecimal.valueOf(3);

    System.out.println(one.divide(three, 10, RoundingMode.DOWN));
    // prints "0.3333333333"

    System.out.println(one.divide(three, 10, RoundingMode.UP));
    // prints "0.3333333334"

    System.out.println(one.divide(three, 333, RoundingMode.UNNECESSARY));
    // throws java.lang.ArithmeticException: Rounding necessary

相关问题

API 链接

  • java.math.RoundingMode
  • java.math.BigDecimal

    <块引用>

    BigDecimal 由任意精度整数未缩放值和 32 位整数 scale 组成。如果为零或正数,则小数位数为小数点右侧的位数。如果为负,则该数字的未缩放值乘以 10 的缩放负次方。

It would've been RoundingMode.HALF_EVEN in Java; it's RoundingMode::HALF_EVEN in Ruby. You may also be able to use the int constants overload (i.e. BigDecimal::ROUND_HALF_EVEN), but the enum overload is definitely the way to go.

You can control the scale of the quotient using the divide(BigDecimal divisor, int scale, RoundingMode mode) overload.

Here's a Java snippet:

    BigDecimal one = BigDecimal.ONE;
    BigDecimal three = BigDecimal.valueOf(3);

    System.out.println(one.divide(three, 10, RoundingMode.DOWN));
    // prints "0.3333333333"

    System.out.println(one.divide(three, 10, RoundingMode.UP));
    // prints "0.3333333334"

    System.out.println(one.divide(three, 333, RoundingMode.UNNECESSARY));
    // throws java.lang.ArithmeticException: Rounding necessary

Related questions

API links

  • java.math.RoundingMode
  • java.math.BigDecimal

    A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale.

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