如何从double中获取整数的小数部分?
How to get decimal parts in integer from double?
如何将double分成两个整数?小数点前的第一个数字和小数点后的第二个数字。
例如:
double doub = 543.345671;
int num1 = (int) doub; //543
int num2 = getNumAfterDecimal(doub-num1); //return 345671
我需要小数部分到整数。
使用 Double.toString()
将 double
映射到 String
: Double.toString(doub)
然后使用 String.split("\.")
将不同的部分映射为 String
秒。然后,可选地,Integer.valueOf()
将这些值解析为 Integer
s :
double doub = 543.345671;
// Here, you want to split the String on character '.'
// As String.split() takes a regex, the dot must be escaped.
String[] parts = Double.toString(doub).split("\.");
System.out.println(Integer.valueOf(parts[0]));
System.out.println(Integer.valueOf(parts[1]));
输出:
543
345671
使用正则表达式拆分获取它,
double doub = 543.345671;
String data[] = String.valueOf(doub).split("\.");
System.out.println("Before decimal: " + Integer.parseInt(data[0]) + ", After decimal: " + Integer.parseInt(data[1]));
打印,
Before decimal: 543, After decimal: 345671
据我所知,
Double d=543.345671;
String line=String.valueOf(d);
String[] n=line.split("\.");
int num1=Integer.parseInt(n[0]);
int num2=Integer.parseInt(n[1]);
这取决于你想要小数点后多少位,但这是要点:
double d = doub - (int)doub; // will give you 0.xyz
int result = d * 1000; // this is for 3 digits, the number of zeros in the multiplier decides the number of digits
这就是我需要的。谢谢,Nir Levy!但有时可能会出现舍入误差。
double doub = 543.04;
int num1 = (int) doub; //543
int num2 = (int) ((doub-num1)*1000000); //39999
我添加了 Math.round() 以正确获取小数部分。
double doub = 543.04;
int num1 = (int) doub; //543
int num2 = (int) Math.round((doub-num1)*1000000); //40000
如何将double分成两个整数?小数点前的第一个数字和小数点后的第二个数字。 例如:
double doub = 543.345671;
int num1 = (int) doub; //543
int num2 = getNumAfterDecimal(doub-num1); //return 345671
我需要小数部分到整数。
使用 Double.toString()
将 double
映射到 String
: Double.toString(doub)
然后使用 String.split("\.")
将不同的部分映射为 String
秒。然后,可选地,Integer.valueOf()
将这些值解析为 Integer
s :
double doub = 543.345671;
// Here, you want to split the String on character '.'
// As String.split() takes a regex, the dot must be escaped.
String[] parts = Double.toString(doub).split("\.");
System.out.println(Integer.valueOf(parts[0]));
System.out.println(Integer.valueOf(parts[1]));
输出:
543
345671
使用正则表达式拆分获取它,
double doub = 543.345671;
String data[] = String.valueOf(doub).split("\.");
System.out.println("Before decimal: " + Integer.parseInt(data[0]) + ", After decimal: " + Integer.parseInt(data[1]));
打印,
Before decimal: 543, After decimal: 345671
据我所知,
Double d=543.345671;
String line=String.valueOf(d);
String[] n=line.split("\.");
int num1=Integer.parseInt(n[0]);
int num2=Integer.parseInt(n[1]);
这取决于你想要小数点后多少位,但这是要点:
double d = doub - (int)doub; // will give you 0.xyz
int result = d * 1000; // this is for 3 digits, the number of zeros in the multiplier decides the number of digits
这就是我需要的。谢谢,Nir Levy!但有时可能会出现舍入误差。
double doub = 543.04;
int num1 = (int) doub; //543
int num2 = (int) ((doub-num1)*1000000); //39999
我添加了 Math.round() 以正确获取小数部分。
double doub = 543.04;
int num1 = (int) doub; //543
int num2 = (int) Math.round((doub-num1)*1000000); //40000