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
8 Respostas
+ 5
@John You should use a for the output. The variable with the casted value.
+ 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.
+ 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.
+ 3
Cast it as an integer. An integer by definition cannot contain a decimal.
+ 3
Casting will do exactly what you want. For example:
double calcInput = 2.09
int castedValue = (int)calcInput
+ 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.
+ 1
Thank you all , I have tried casting and it works.
0
I want it for a calculator program, so sometimes the user want to insert a double number like (2,6).