Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7843275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:30:50+00:00 2026-06-02T16:30:50+00:00

So I am currently reading a book on learning java and a program outlined

  • 0

So I am currently reading a book on learning java and a program outlined inside the book is giving me some trouble. Specifically when I run the program to compute the amount of dollars and change that I should have from 9.87, I get change from what I would get as if I typed 9.86. However when I type 9.86 I get the correct amount of change. As far as I know this only happens with some amounts that end in .87 such as 8.87, although it works for 6.87 fine. This is the book introduction to java programming 8th edition and since it was a book that was written by a professional I am confused as to what the error could be. I have tried running it in cmd and eclipse and both seem to show the fault.

PasteBin Link:

http://pastebin.com/CVssSUYp

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:30:52+00:00Added an answer on June 2, 2026 at 4:30 pm

    I’m almost certain that you’re being bitten by a poor understanding of how floating point numbers work. You can no more represent 0.1 exactly in binary any more than you can 1/3 in decimal. Then, on top of that, IEEE floating point numbers can’t have more than 17 digits of precision for doubles.

    It’s not a bug in Java or your code.

    Money is a classic example of something that shouldn’t be represented with decimal numbers.

    Write a Money class with integer dollars and cents and use that when you learn enough Java:

    public class Money {
        private final int dollars;
        private final int cents;
        private final Currency currency;
    
        public Money(int dollars, int cents, Currency curr) {
            if (dollars < 0) throw new IllegalArgumentException("dollars cannot be negative");
            if (cents < 0) throw new IllegalArgumentException("cents cannot be negative");
            if (curr == null) throw new IllegalArgumentException("currency cannot be null");
            this.dollars = dollars; 
            this.cents = cents;
            this.currency = curr;
        }
        // more methods here.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading a book about AI and currently learning about pathfinding(currently doing the Dijkstra
Currently I have listed down learning Unix/Shell Scripting and reading CLR Algo book as
I'm currently learning jQuery by reading jQuery in Action. The book discusses separation of
I am currently reading a book, and learning ruby on rails. (Agile Web Development
im currently reading a book about programming Android and there is a nice little
I'm currently reading the book Pro Asp.Net MVC Framework. In the book, the author
I'm currently reading the book Professional Enterprise .NET and I've noticed this warning in
Currently reading quite a heavy WCF book. I have used it myself in production
Hi, guys! Currently I'm reading a book Programming in Objective-C by Stephen Kochan. Actually
I am currently reading a book about bit fiddling and the following formula appears:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.