Results 1 to 6 of 6
  1. #1
    Nicke is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Question Roudning problems

    Hi

    I just started to develop in java at my company and ran into a rounding problem that i can't solve as i like.
    Got this number, 69,1999999999998 i want to round so i get 69,20.
    But the result end up 69.2. I want my number to keep the zero.

    My code looks like this
    .........

    var number: int = 69,1999999999998

    number = Math.round(100000 * number) / 100000;

    //number = 69.2

    Does anyone know what i should do?
    FYI, i'm calculating amounts on articles and want to keep the correct decimal value for the currency denomination.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    How are you displaying the number? The printf() method has formatting controls that will give you what you want.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You understand of course that computers don't represent floating point numbers with exact precision (with exceptions of course), that what you're seeing isn't an error but is inherent in any programming language that uses floating points. If accuracy is of paramount importance, such as when dealing with financial numbers, then don't use doubles but instead use BigDecimals. More often though the accuracy of double is adequast and what is more important is probably how you display the results (which is what Norm was getting at above).

  4. #4
    Nicke is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default

    We are using "Alert" to popup the message.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    Is that javascript you're working with? Or java?
    What class/mehtod is Alert ?

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by Nicke View Post
    Hi

    I just started to develop in java at my company and ran into a rounding problem that i can't solve as i like.
    Got this number, 69,1999999999998 i want to round so i get 69,20.
    But the result end up 69.2. I want my number to keep the zero.

    My code looks like this
    .........

    var number: int = 69,1999999999998

    number = Math.round(100000 * number) / 100000;

    //number = 69.2

    Does anyone know what i should do?
    FYI, i'm calculating amounts on articles and want to keep the correct decimal value for the currency denomination.
    var number smells like javascript and your talk of "alert" confirms it, you got the wrong forum, java is not javascript, javascript got its name from netscape who decided to use the name java as a prefix to its script because at the time of development java was seriously hot and they decided to jump on the bandwagon causing silly confusion to the likes of you.

Similar Threads

  1. problems with Jxl
    By Harpreet1111 in forum New To Java
    Replies: 1
    Last Post: 07-07-2010, 07:27 PM
  2. Few Problems Help!
    By elad_bj in forum New To Java
    Replies: 4
    Last Post: 04-07-2009, 03:45 AM
  3. Problems in JSP : Need help
    By raj4u in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-07-2008, 10:06 AM
  4. many to many problems
    By cecily in forum JDBC
    Replies: 1
    Last Post: 08-02-2007, 05:51 PM
  5. problems with JPA
    By Ed in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 05:34 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
  •