views:

135

answers:

2

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?
A: 
polygenelubricants
In Ruby, to access static / class constants the notation is '::'. And JRuby automatically transalate the Java syntax to Ruby syntax accordingly.
DJ
@DJ: thanks for the comment =) Feedback incorporated.
polygenelubricants
Thanks! it works. a small follow up question - now when I divide 1/3 it returns 0 or 1 (according to rounding defined). is there a way that I can tune the accurate fraction, for example 1/3 = 0.3333 or 5/7=0.7142
Eyal
@Eyal: use the `scale` parameter. See added examples.
polygenelubricants
Great, thanks again!
Eyal
+1  A: 

Try

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