Results 1 to 4 of 4

Thread: String/Number formatting

Threaded View

  1. #1

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Resolved String/Number formatting

    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.
    Code:
    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"
    Last edited by x-ice; Mar 27th, 2008 at 11:49 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width