I am making a graphical calculator program in Java and have a method that performs an operation based on user input, and returns a double to be displayed in a JTextField
. However, I would like the result to be displayed as a whole number, without a following ".0"
where there are no significant digits after the decimal point, but still display decimal points when necessary. What's the most efficient way of doing this?
Compare two values: the normal double, and the double after floor ing it. If they are the same value, there is no decimal component.
Go to Conditional Formatting > New rule from home ribbon. Select rule type as “Use a formula…” Check if there is a value after decimal point using a formula like =Mod(A1,1)>0. Click the format button.
You can use DecimalFormat
for suppressing the trailing characters, e.g.:
DecimalFormat format = new DecimalFormat("#.##");
double doubleFromTextField = Double.parseDouble(myField.getText());
System.out.println(format.format(doubleFromTextField));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With