如何在Ruby中将浮点十进制值转换为整数 [英] How to turn float decimal values into integers in ruby
本文介绍了如何在Ruby中将浮点十进制值转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取浮点数的十进制值并将其转换为ruby中的整数?
How can one get the decimal values of a float and turn it into an integer in ruby?
这是我到目前为止所走的距离:
Here is how far I got so far:
number = 12.55
decimal_length = number.to_s.split('.')[1].size #=> 2
decimal = number.divmod(1)[1].round(decimal_length) #=> 0.55
这里如何将小数转换为整数(55),使其可以与任何数字作为输入?
Here how can I turn decimal into integer (55) in a way that it would work with any number as input?
推荐答案
也许我误会了?但是您已经知道,您只是在做额外的工作.
Maybe I'm misunderstanding? But you've got it you're just doing extra work.
number = 12.55
number.to_s.split('.')[1].to_i
# => 55
这篇关于如何在Ruby中将浮点十进制值转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文