Results 1 to 8 of 8
- 04-06-2014, 03:15 AM #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?
- 04-06-2014, 03:23 AM #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 classLast edited by Norm; 04-06-2014 at 02:32 PM.
If you don't understand my response, don't ignore it, ask a question.
- 04-06-2014, 03:55 AM #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) { }
- 04-06-2014, 04:41 AM #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);
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 05:05 AM.
- 04-06-2014, 02:13 PM #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?If you don't understand my response, don't ignore it, ask a question.
- 04-06-2014, 02:45 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: formatting double to fixed number of fractional digits
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 04-06-2014, 03:45 PM #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.
- 04-06-2014, 03:47 PM #8
Similar Threads
-
Set number of significant digits
By dojob in forum New To JavaReplies: 18Last Post: 08-24-2013, 06:34 AM -
How to enclose certain digits of a value double in parentheses
By herpeslurpy in forum New To JavaReplies: 12Last Post: 01-17-2013, 08:25 AM -
Telephone Number To Digits
By irnie1994 in forum JCreatorReplies: 1Last Post: 10-07-2011, 05:15 PM -
need help with Number Pyramid with Double Digits
By SmellyFoot in forum New To JavaReplies: 5Last Post: 03-29-2011, 01:04 PM -
Separating the digits of given number
By lb2 in forum New To JavaReplies: 5Last Post: 09-09-2010, 06:29 AM
Bookmarks