将double分成整数和小数部分 [英] Separating double into integer and decimal parts

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

问题描述

我试图将double分成整数和小数部分

I am trying to separate a double into the integer and decimal parts

例如,数字24.4应分为24和4。

So for example, the number 24.4 should be separated into 24 and 4.

int integer = (int)number;
double decimal = number-(int)number;
System.out.println(decimal); 

使用此方法可以得到以下值:

Using this method gives me the following values :

integer = 24
decimal = 0.3999999999999986

我需要十进制值为4.

I need the value of decimal to be 4.

如何修复此问题?

推荐答案

你可以做一个String split(...) 。然后是Integer parseInt(...) 取回两个整数组件。

You could do a String split(...). And then Integer parseInt(...) to get back the two integer components.

这篇关于将double分成整数和小数部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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