# Thread: formatting double to fixed number of fractional digits

1. Member
Join Date
Mar 2013
Posts
20
Rep Power
0

## 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. ## 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.

3. Member
Join Date
Mar 2013
Posts
20
Rep Power
0

## 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. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## 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. ## 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?

6. ## Re: formatting double to fixed number of fractional digits

Originally Posted by hill0093
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

7. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## 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. ## 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.

#### Posting Permissions

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