Reputation: 149
I am making a simple calculator and currently trying to fix issues with non-integers.
Got a text field displayField
showing the results and operator buttons as well as an equal button.
Just got it working that the double results only show decimal places if there are any but I cannot get the results back into the calculation.
public class FXMLDocumentController implements Initializable {
private String operator;
double oldValue;
double newValue = 0;
NumberFormat nf = new DecimalFormat("##.###");
@FXML
private TextField displayField;
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
@FXML
private void handleDigitAction(ActionEvent event) {
String digit = ((Button) event.getSource()).getText();
String oldText = displayField.getText();
String newText = oldText + digit;
displayField.setText(newText);
}
@FXML
private void handleOperator(ActionEvent event) {
oldValue = Double.parseDouble(displayField.getText());
displayField.setText("");
operator = ((Button) event.getSource()).getText();
}
@FXML
private void handleEqualAction(ActionEvent event) {
switch (operator) {
case "+":
newValue = oldValue + Double.parseDouble(displayField.getText());
break;
case "-":
newValue = oldValue - Double.parseDouble(displayField.getText());
break;
case "*":
newValue = oldValue * Double.parseDouble(displayField.getText());
break;
case "/":
newValue = oldValue / Double.parseDouble(displayField.getText());
break;
default:
break;
}
displayField.setText(String.valueOf(nf.format(newValue)));
}
}
The error occurs when I for example try to compute 5/2 first, get the result 2,5 and then hit an operator button. So I assume I just have to either use an additional object to save the results or just change the line where I read from the text field (so that it also works for this changed format) but I cannot figure out how.
Upvotes: 0
Views: 539
Reputation: 441
You are probably using DecimalFormat
class to format the output. The decimal formater uses the default Locale
which in your case is de_DE
.
As mentioned in the above answers you can use the overloaded method of the DecimalFormat
class to get the output in your required format.
E.g.
BigDecimal numerator = new BigDecimal(5);
BigDecimal denominator = new BigDecimal(2);
//In current scenario
Locale locale = new Locale("de", "DE");
NumberFormat format = DecimalFormat.getInstance(locale);
String number = format.format(numerator.divide(denominator));
System.out.println("Parsed value is "+number);
The output here will be 2,5
If you change to:
Locale localeDefault = new Locale("en", "US");
NumberFormat formatDefault = DecimalFormat.getInstance(localeDefault);
String numberVal = formatDefault.format(numerator.divide(denominator));
System.out.println("Parsed value is "+numberVal);
Output here will be 2.5
Hope this helps.
Upvotes: 1
Reputation: 149
I found a rather "simple" or dirty solution, which seems to work:
NumberFormat nf = new DecimalFormat("##.###", new DecimalFormatSymbols(Locale.US));
Upvotes: 1
Reputation: 1010
When you use the format()
method of a NumberFormat
(or its subclass DecimalFormat
), you happen to be using either the default Locale
or a Locale
you pass the method, depending on the overload you use. As a result, you get a Locale
-formatted output.
The same way, you should use your DecimalFormat
's parse()
method to parse your display field according to the same rules.
I hope this will help...
Jeff
Upvotes: 1
Reputation: 83
Can you tell us which locale is your application running on?
Execute
System.out.println(Locale.getDefault());
Upvotes: 1