Results 1 to 19 of 19
  1. #1
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Set number of significant digits

    I have came across a problem where I have to display an output to a double to number of significant digits.

    Eg: round to 3 significant digits
    12345 -> 12300
    2.4134 -> 2.14

    I read through the Java document on Decimal Format but i could not found any way on reducing double to significant digits.
    Is there any java built in on this?
    DecimalFormat (Java Platform SE 7 )

  2. #2
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: Set number of significant digits

    Isn't #.## is for formatting number of decimal places? it does not format number of significant digits.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Set number of significant digits

    For formatting to display a specified number of significant digits, as far as I know there isn't anything for that in the standard JDK. You may be able to find a ready made solution in one of the many open source projects, and failing that would need to roll your own. I would probably go with scaling the number till I had the required number of digits to the left of the decimal point, converting that to an int value and then applying formatting rules for the scaling factor.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Set number of significant digits

    Quote Originally Posted by dojob View Post
    Isn't #.## is for formatting number of decimal places? it does not format number of significant digits.
    yup, it was for decimal places...for significant digits... you can define your own algos ...

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Set number of significant digits

    allaudin, don't delete content that has been responded to as that makes a forum thread difficult to follow for those who come later.

    If your response was in error and is pointed out, it's best to edit the post and add a note to that effect.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Set number of significant digits

    Quote Originally Posted by DarrylBurke View Post
    allaudin, don't delete content that has been responded to as that makes a forum thread difficult to follow for those who come later.
    Or even better: don't try to answer at all if you're not sure about the correctness of the answer or if you haven't tested the code you supplied; it minimizes the noise in this forum.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,691
    Rep Power
    5

    Default Re: Set number of significant digits

    Quote Originally Posted by JosAH View Post
    Or even better: don't try to answer at all if you're not sure about the correctness of the answer or if you haven't tested the code you supplied; it minimizes the noise in this forum.

    kind regards,

    Jos
    Wagging finger: I did that too in the early years, only I just phrased myself differently; in stead of making it look I was answering, I was proposing a possible solution and asking for feedback on its correctness. I learned a great deal by being corrected that way. Its no good for your education to not post at all, IMO. Just make sure you show the proper intentions.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Set number of significant digits

    Quote Originally Posted by gimbal2 View Post
    Wagging finger: I did that too in the early years, only I just phrased myself differently; in stead of making it look I was answering, I was proposing a possible solution and asking for feedback on its correctness. I learned a great deal by being corrected that way. Its no good for your education to not post at all, IMO. Just make sure you show the proper intentions.
    I disagree; not only are repliers (who do know what they talking about) tempted to answer the OP but also correct the answer given by replier(s) who don't know what they're talking about.It obfusctes the original discussion in the thread. One can also learn by reading topics in the forum(s); there is no need to try to answer stuff one doesn't know about (yet).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,691
    Rep Power
    5

    Default Re: Set number of significant digits

    We'll have to agree to disagree then :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Set number of significant digits

    Quote Originally Posted by gimbal2 View Post
    We'll have to agree to disagree then :)
    Your mother was a hamster and your father smelt of elderberries; so there ...

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,691
    Rep Power
    5

    Default Re: Set number of significant digits

    Quote Originally Posted by JosAH View Post
    I am rubber, you are glue.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Set number of significant digits

    Quote Originally Posted by gimbal2 View Post
    I am rubber, you are glue.
    Go wash your mouth, you rascal!

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,691
    Rep Power
    5

    Default Re: Set number of significant digits

    Quote Originally Posted by JosAH View Post
    Go wash your mouth, you rascal!
    *jiggles a Grolsch in front of JosAH*

    Here boy! Come get it! GO FETCH! *toss*
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Set number of significant digits

    Quote Originally Posted by gimbal2 View Post
    *jiggles a Grolsch in front of JosAH*

    Here boy! Come get it! GO FETCH! *toss*
    <starts drooling>
    Arf! Arf! Arf!
    <starts drooling/>

    kind regards,

    Jos

    ps. that is alcohol abuse, don't ever do that again ;-)
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,691
    Rep Power
    5

    Default Re: Set number of significant digits

    Sorry. Friday-mischievousness.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Set number of significant digits

    Quote Originally Posted by gimbal2 View Post
    Sorry. Friday-mischievousness.
    Do as I do: work from home; it is extremely comfy with weather like this ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,691
    Rep Power
    5

    Default Re: Set number of significant digits

    What kind of effect would that have on my Friday-mischievousness? I'm a prick wherever I am.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Set number of significant digits

    Quote Originally Posted by gimbal2 View Post
    What kind of effect would that have on my Friday-mischievousness? I'm a prick wherever I am.
    Nah, can't be; in my garden you can feel the sun, hear the birds sing, see the fish play and most important: there are ample supplies of cold Grolsch beer in the barn whenever you need them.

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: Set number of significant digits

    Guess I would have to work it out using own defined function. Thanks for the reply

Similar Threads

  1. Telephone Number To Digits
    By irnie1994 in forum JCreator
    Replies: 1
    Last Post: 10-07-2011, 04:15 PM
  2. 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
  3. Separating the digits of given number
    By lb2 in forum New To Java
    Replies: 5
    Last Post: 09-09-2010, 05:29 AM
  4. Count number of digits in string using scanner
    By wendysbiggy in forum New To Java
    Replies: 35
    Last Post: 01-20-2010, 05:11 AM
  5. rounding a double in a number of significant numbers
    By rikribbers in forum Advanced Java
    Replies: 2
    Last Post: 10-27-2008, 03:35 PM

Posting Permissions

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