Capping decimal places
The above code takes two values add's 0.07 and 20.00 to it and displays it in a textbox, I was wondering if there was anyway to "cap" the decimals to 2 deimcal places, aka currency format.
int previous = Integer.parseInt(TxtPrevious.getText());
int current = Integer.parseInt(TxtCurrent.getText());
double cpu = 0.07;
double quart = 20.00;
if (current >= previous)
double sum = (current - previous) * cpu + quart;
String sAnswer = Double.toString(sum);
TxtResult.setText ("£" + sAnswer);
JOptionPane.showMessageDialog(null, "Current reading has to be greater than previous reading", "Billing Error", JOptionPane.ERROR_MESSAGE);
Check DecimalFormat class.