Ruby:以科学记数法控制打印 [英] Ruby: Controlling printing in scientific notation
本文介绍了Ruby:以科学记数法控制打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我在 Ruby 中有一个非常长的浮点数,例如:
If I have an extremely long floating point number in Ruby such as:
x = 123456789012345.to_f
当它显示时,比如通过 to_s
,它以科学计数法出现:
when it is displayed, say, via to_s
, it appears in scientific notation:
"1.23456789012345e+14"
有没有办法抑制科学记数法的格式,或者在硬币的另一面,强制使用极短的浮点数?
Is there any way to suppress the formatting in scientific notation, or on the other side of the coin, force it for extremely short floating point numbers?
推荐答案
您可以使用 %
运算符.例如:
You can do all sorts of things using the %
operator. For example:
x = 123456789012345.to_f
"%f" % x # => "123456789012345.000000"
y = 1.23
"%E" % y # => "1.230000E+000"
各种选项与 sprintf 函数相同.
The various options are the same as for the sprintf function.
这篇关于Ruby:以科学记数法控制打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文