很抱歉,如果这是显而易见的,但我搜索Google,Stack Overflow和Ruby文档,我找不到它。
我如何制作更高精度的十进制数?例如,在IRB中,
irb(main):001:0> 3.14159265358979323846
=> 3.141592653589793
它会切断一些小数。如何保留整个小数?
答案 0 :(得分:3)
您需要使用BigDecimal
课程:http://www.ruby-doc.org/stdlib-1.9.3/libdoc/bigdecimal/rdoc/BigDecimal.html
BigDecimal提供任意精度浮点十进制算法。
示例:
irb(main):009:0> BigDecimal.new("654.687465465496876516874651463549867651")
=> #<BigDecimal:2da6878,'0.6546874654 6549687651 6874651463 549867651E3',45(54)>
答案 1 :(得分:1)
试试这个:
require 'bigdecimal/math'
include BigMath
puts PI(70) #You'll get a few more digits above 70, but those will be off. 70 is by no means the maximum.
#=> 0.314159265358979323846264338327950288419716939937510582097494459230781640628620899862802532985155833326733E1