Results 1 to 8 of 8
  1. #1
    hill0093 is offline Member
    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0

    Default formatting double to fixed number of fractional digits

    Sorry to ask such a simple question.
    Where do I find a subroutine similar to this:
    String formatDouble(double dblNum,int NumOfDecDigits) { }
    For years I have been using output subroutines I wrote long ago.
    Now I need this kind and can't remember where it is.
    How do I print doubles to a fixed number of digits to right of the point?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: formatting double to fixed number of fractional digits

    There are a couple of ways to format a double for printing:
    The DecimalFormat class
    The printf() method of PrintStream
    The format() method of String class
    Last edited by Norm; 04-06-2014 at 01:32 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    hill0093 is offline Member
    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0

    Default Re: formatting double to fixed number of fractional digits

    both of those classes seem too complicated
    I just want a subroutine similar to this:
    StringBuffer formatDouble(double dblNum,int NumOfDecDigits) { }

  4. #4
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: formatting double to fixed number of fractional digits

    Try something like this:
    Java Code:
        String s = String.valueOf(num).substring(0,s.indexOf(".") + digits + 1);
    Where digits is how many digits after the decimal, and num is your float.

    Convert float to a string, then take a substring starting from the beggining, and stopping the correct number of digits after the decimal point (the + 1 is there because substring's end point is exclusive, so you have to add one to take the last digit you want).

    Simply make that into a method with the parameters of double num and int digits, and you're set.
    Last edited by AlexGraal; 04-06-2014 at 04:05 AM.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: formatting double to fixed number of fractional digits

    @AlexGraal Did you try to compile that line of code? What happened?
    What does that code do with num=1.0 and digits = 2?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: formatting double to fixed number of fractional digits

    Quote Originally Posted by hill0093 View Post
    both of those classes seem too complicated
    I just want a subroutine similar to this:
    StringBuffer formatDouble(double dblNum,int NumOfDecDigits) { }
    They're not complicated after you've read their API documentation; depending on which of the classses you want to use, all you have to do is use a pattern like "0.0000" or a format string "%1.4f"; I don't see any complicated stuff here (except for the other hackish 'solutions')

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: formatting double to fixed number of fractional digits

    Yeah! I wrote a small method with some checks to prevent that too, but in the spirit of not giving away full code, I took it all out and just posted that.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: formatting double to fixed number of fractional digits

    Once the a full, correctly working method is written, it will probably be more complicated than using one of the built in formatting methods.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Set number of significant digits
    By dojob in forum New To Java
    Replies: 18
    Last Post: 08-24-2013, 05:34 AM
  2. Replies: 12
    Last Post: 01-17-2013, 07:25 AM
  3. Telephone Number To Digits
    By irnie1994 in forum JCreator
    Replies: 1
    Last Post: 10-07-2011, 04:15 PM
  4. need help with Number Pyramid with Double Digits
    By SmellyFoot in forum New To Java
    Replies: 5
    Last Post: 03-29-2011, 12:04 PM
  5. Separating the digits of given number
    By lb2 in forum New To Java
    Replies: 5
    Last Post: 09-09-2010, 05:29 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
  •