0

remove zero from double

Hello, can you help me please: How can i remove the (0) from a double number. Double a=2,00; I want it to be (2) only

30th Aug 2017, 12:14 AM
John
8 odpowiedzi
+ 5
@John You should use a for the output. The variable with the casted value.
30th Aug 2017, 2:29 PM
Tashi N
Tashi N - avatar
+ 9
For outputs: import java.text.*; public class Program{ public static void main(String[] args) { double d = 4.0; NumberFormat formatter = new DecimalFormat("#0,0"); System.out.println(formatter.format(d)); } } ... and vice versa for inputs.
30th Aug 2017, 6:01 AM
Tashi N
Tashi N - avatar
+ 5
Keep it as a double, if it rounds with a .00 then cast it to an int. Example/ double exampleInput = 7 + 2; // this will become 9.0 int temp = (int) exampleInput; if (temp - exampleInput < 0.0001) // Use temp else // Use double I wrote < 0.0000...1 just to assure floating precision isn't an issue.
30th Aug 2017, 3:42 AM
Rrestoring faith
Rrestoring faith - avatar
+ 3
Cast it as an integer. An integer by definition cannot contain a decimal.
30th Aug 2017, 12:20 AM
Blaise Cannon
+ 3
Casting will do exactly what you want. For example: double calcInput = 2.09 int castedValue = (int)calcInput
30th Aug 2017, 12:33 AM
Blaise Cannon
+ 1
if it's for a calculator then it might be best to keep it as a double since not every equation well yield a whole number as the answer.
30th Aug 2017, 1:56 AM
Christian
Christian - avatar
+ 1
Thank you all , I have tried casting and it works.
30th Aug 2017, 2:38 PM
John
0
I want it for a calculator program, so sometimes the user want to insert a double number like (2,6).
30th Aug 2017, 12:29 AM
John