Results 1 to 3 of 3
  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default setting format for FLoat values

    Hi Guys

    Another question :D

    I have a float variable that has calculated average of few numbers in it. I want that float variable to keep 4 digits after decimal places. How can this be done:confused:

    Cheers

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    10

    Default

    Depends on what you're doing. If you can't get four places because of decimal location or precision issues you can use double. For presentation you can use NumberFormat:
    Java Code:
    // see NumberFormat api for options.
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMinimumFractionDigits(4);
    nf.setMaximumFractionDigits(4);
    String s = nf.format(myFloatValue);
    // or, in j2se 1.5+ you can specify precision
    s = String.format("%.4f", yourFloatValue);
    DecimalFormat is another class that some use for this.

  3. #3
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 04-15-2008, 01:04 PM
  2. String to Float
    By durahman in forum New To Java
    Replies: 2
    Last Post: 02-12-2008, 01:17 AM
  3. Reading Float values from a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:20 AM
  4. Float to String
    By mew in forum New To Java
    Replies: 4
    Last Post: 12-29-2007, 06:08 PM
  5. Class float
    By Peter in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:17 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
  •