获取十进制数的小数部分 [英] get fraction part of a decimal number

查看:48
本文介绍了获取十进制数的小数部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 rails 中获取十进制数的小数部分.例如,我有一个数字,即1.23",我想得到23"这可能太容易了,但是,有人知道我该怎么做吗?

I am trying to get a fraction part of a decimal number in rails. For example I have a number, that "1.23" and I want to get "23" It is may be too easy but, does anyone have any idea about how can I do?

推荐答案

我不确定这是否是最简单的方法 - 但您可以简单地使用."分割数字.字符 - 像这样:

I am not sure if it is the easiest way to do it - but you can simply split the number using "." character - like this:

number = 1.23
parts = number.to_s.split(".")
result = parts.count > 1 ? parts[1].to_s : 0

这篇关于获取十进制数的小数部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆