How can I format a double value such that it has no decimal places at all ie it must look like an integer. I need this because I want to create a double array but there are some integers in the array as well, and they look untidy in my gui eg 500.0 I want to know how to get rid of that 0.
如何格式化double值,使其根本没有小数位,即它必须看起来像一个整数。我需要这个,因为我想创建一个双数组,但是数组中也有一些整数,并且它们在我的gui中看起来不整洁,例如500.0我想知道如何摆脱那个0。
2 个解决方案
#1
If I understood correctly, why not just cast your doubles into integers?
如果我理解正确,为什么不把你的双打投入整数?
System.out.println(2); // prints 2
System.out.println(2.242); // prints 2.242
System.out.println((int)2.242); // prints 2
System.out.println((int)2.999); // still prints 2
Mind you, you will need to be careful if you are working with really big numbers.
请注意,如果你正在使用非常大的数字,你需要小心。
System.out.println((int)985652700000.9989); // prints 2147483647 due to overflow
System.out.println((long)985652700000.9989); // prints 985652700000
#2
/*
Mira, puedes asignar el valor de una variable 'double'
a otra 'int' aplicando un cast, exactamente como ya lo dijo *CSCH*.
Ejemplo:
*/
private void mostrarValores() {
double d = 123.05;
int i = (int) d;
System.out.println("Valor int: " + i + "\nValor double: " + d);
}
/*
Eso imprime en consola:
Valor int: 123
Valor double: 123.05
BUILD SUCCESSFUL (total time: 0 seconds)
*/
#1
If I understood correctly, why not just cast your doubles into integers?
如果我理解正确,为什么不把你的双打投入整数?
System.out.println(2); // prints 2
System.out.println(2.242); // prints 2.242
System.out.println((int)2.242); // prints 2
System.out.println((int)2.999); // still prints 2
Mind you, you will need to be careful if you are working with really big numbers.
请注意,如果你正在使用非常大的数字,你需要小心。
System.out.println((int)985652700000.9989); // prints 2147483647 due to overflow
System.out.println((long)985652700000.9989); // prints 985652700000
#2
/*
Mira, puedes asignar el valor de una variable 'double'
a otra 'int' aplicando un cast, exactamente como ya lo dijo *CSCH*.
Ejemplo:
*/
private void mostrarValores() {
double d = 123.05;
int i = (int) d;
System.out.println("Valor int: " + i + "\nValor double: " + d);
}
/*
Eso imprime en consola:
Valor int: 123
Valor double: 123.05
BUILD SUCCESSFUL (total time: 0 seconds)
*/