-1

currently' im doing on a project regarding ordering system. However, whenever the price of the food calculate together, it wil display a string of decimal. I only want to display up tp 2 decimal point. After researching on how to use decimal format, im still at loss.may i know how to resolve this prblem?

    private double getTotalPrice(){   
        total = 0;
        for(Food fd:foodList){
     total += fd.getFoodPrice()*fd.getQuantity();
        PrintStream printf = System.out.printf("Total:$%.2f ", total);


    }

    return total;

   }
    private void setTableModel() {
    FoodTableModel model = new FoodTableModel(foodList);
    totalAmount = getTotalPrice();
    String s = String.valueOf(totalAmount);
    txtPrice.setText(s);

    tblList3.setModel(model);
    tblList3.removeColumn(tblList3.getColumnModel().getColumn(0));


  }

1 Answers1

1

As Boris the Spider said in his comment, you should not be using floating point numbers to store prices.

So here is an answer in two parts:

What you did wrong

You wanted to get a rounded string value from a floating point number, but instead, you just printed that rounded value, and still returned double from getTotalPrice().

Printing doesn't change the actual value, it just displays it on the console. If you then return the double value, it is still a double value, and converting it to string will not "know" that you have printed it rounded in the past.

So your method should not print anything:

private double getTotalPrice(){   
    total = 0;
    for(Food fd:foodList){
        total += fd.getFoodPrice()*fd.getQuantity();
    return total;
}

Instead, you should format it when you get it:

totalAmount = getTotalPrice();
String s = String.format( "%.2f", totalAmount );

What you should have done

You should represent all the prices in the system in cents. That is, instead of using 12.34 as the price of something, use 1234.

Then, you have to convert the amount back and forth from dollars to cents. When you put the value in a field, you have to convert it to dollars, and when you get it back into a system for calculations, you should convert it back to cents. For example:

private static final Pattern moneyFormat = Pattern.compile("(\\d+)\\.(\\d\\d)");

private static String centsToDollars( int cents ) {
    return String.format( "%d.%02d", cents / 100, cents % 100 );
}

private static int dollarsToCents( String dollars ) {
    Matcher matcher = moneyFormat.matcher(dollars);
    if ( ! matcher.matches()) {
        throw new IllegalArgumentException(dollars + " is not a proper dollar amount");
    }
    return Integer.parseInt(matcher.group(1)) * 100 + Integer.parseInt(matcher.group(2));

}

Of course, you can make your pattern more lenient to allow things like -15.3 or .32 etc. - mine only allows positive dollar amounts with two digits after the dot.

So in this case you'd be totaling in integer:

private int getTotalPrice(){   
    total = 0;
    for(Food fd:foodList){
        total += fd.getFoodPrice()*fd.getQuantity();
    return total;
}

And displaying in string:

totalAmount = getTotalPrice();
String s = centsToDollars(totalAmount);

There are also other alternatives such as using BigDecimal.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79