I am facing a problem with BigDecimal.
This code:
x = BigDecimal.new('1.0') / 7
puts x.to_s
outputs:
0.142857142857142857E0
I want to increase the number of digits.
In JAVA, I could do:
BigDecimal n = new BigDecimal("1");
BigDecimal d = new BigDecimal("7");
n = n.divide(d,200, RoundingMode.HALF_UP);
System.out.println(n);
The output is:
0.1428571428571428571428571428571428571428571428571428571428... (200 digits)
I looked at BigDecimal documentation, and tried to set the digits when instantiating the number, then tried to set the limit with the BigDecimal.limit, but I couldn't print more than 18 digits.
What am I missing?
I am running ruby 1.9.3p0 (2011-10-30) [i386-mingw32] on Windows 7 64bits
The div
method allows you to specify the digits:
x = BigDecimal.new('1.0').div( 7, 50 )
puts x
With a result of:
0.14285714285714285714285714285714285714285714285714E0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With