x-ice
Mar 27th, 2008, 05:56 AM
How can I format a number in java so that only two numbers are displayed after the decimal point?
I have tried using DecimalFormat with no luck.
Here is the code I used. float newTotPrice = totPrice + Double.valueOf(myProduct.getPrice()).floatValue();
//For formatting a number
DecimalFormat df = new DecimalFormat("#.00");
//Format 'newTotPrice' so that only two digits are shown after the decimal point
df.format(newTotPrice);
//Show total price
lblTotPrice.setText("£ " + newTotPrice); This doesn't work as I got a result of "£ 509.97003"
I have tried using DecimalFormat with no luck.
Here is the code I used. float newTotPrice = totPrice + Double.valueOf(myProduct.getPrice()).floatValue();
//For formatting a number
DecimalFormat df = new DecimalFormat("#.00");
//Format 'newTotPrice' so that only two digits are shown after the decimal point
df.format(newTotPrice);
//Show total price
lblTotPrice.setText("£ " + newTotPrice); This doesn't work as I got a result of "£ 509.97003"