Results 1 to 4 of 4

Thread: control decimal places

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    control decimal places

    Is there a way that I can control the number of decimal places a number has?

    For instance if a calculation = 5.42343423456453454

    can I make this 5.42?

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: control decimal places

    Look into the java.text.DecimalFormat class. There is a method that allows you to set the maximum amount of decimal places. Im pretty sure you can also apply a pattern using the DecimalFormat class.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: control decimal places

    Here's some code to give you an idea.
    Code:
     NumberFormat nf = NumberFormat.getNumberInstance();
     DecimalFormat df = (DecimalFormat)nf;
     df.applyPattern("###.##"); 
     double calculation = 5.42343423456453454;
     System.out.println(calculation);
     System.out.println(df.format(calculation));

  4. #4

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: control decimal places

    Thank you very much.

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