Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Why does 16.33%10 return 6.329999999999998?

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why does 16.33%10 return 6.329999999999998?

    Is there a way to fix this?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Why does 16.33%10 return 6.329999999999998?

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Show your code that presents those results, and then we'll know better what to recommend.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why does 16.33%10 return 6.329999999999998?

    My code is:
    System.out.println(16.33%10);
    and the result is
    6.329999999999998

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Why does 16.33%10 return 6.329999999999998?

    Two options which can be combined or used separately as you'd like:

    1. Format the output to the desired precision, or
    2. Cast the resulting value to a primitive that provides the desired precision.

    Why it happens can be found in many articles on the 'net. Look for something like 'java floating point precision', or 'java currency accuracy'.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why does 16.33%10 return 6.329999999999998?

    Also see: What Every Computer Scientist Should Know About Floating-Point Arithmetic
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. can i return two values from a return method?
    By tonu in forum Object Oriented Programming
    Replies: 4
    Last Post: January 1st, 2014, 11:02 AM
  2. Replies: 1
    Last Post: July 29th, 2013, 06:22 AM
  3. return help
    By Kalle in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 21st, 2012, 05:33 PM
  4. Return Object does not return the expected output
    By Nour Damer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2011, 07:24 AM
  5. how to return value ..
    By gr8mind in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 11th, 2011, 05:27 AM

Tags for this Thread