Simple CashRegister program behaviour issue [Basic]
Hello,
I am writing a simple Java code, which acts like a Cash Register, However I have faced an issue which I hope to get some help on.
The program:
Code :
public class TestItem
{
private double Order; // instant variable which will be representing the item's price
private double Paid; // Paid is an instant variable which represent the amount paid for the item
/**
* Constructs a Cash Register with zero values
*/
public TestItem()
{
Order = 0;
Paid = 0;
}
/**
* Initiating the price of the item ordered in the cash register
* @param total is the price of the item
*/
public void recordOrder(double total)
{
this.Order = Order + total;
}
/**
* Deposit the amount paid by the costumer to the cash registrer
* @param dollars is the amount paid
*/
public void enterPaidValue(double dollars)
{
Paid = dollars;
}
/**
* Calculate then return the change to the cosutmer
*/
public double giveChange()
{
double change = Paid - Order;
Order = 0; // after the calculation is done, reset the register value, ready to serve a new costumer
Paid = 0; // after the calculation is done, reset the register value, ready to serve a new costumer[/COLOR]
return change; // return the change
}
}
Well yes it is easy as it looks, now after coding the tester program:
Code :
/**
* This class is a tester class
*/
public class TestItemTester
{
/**
* Main method.
*/
public static void main(String[] args)
{
TestItem register = new TestItem(); // creating a new object of the class TestItem
register.recordOrder(19.93); // calling the method recordOrder and initiating parameters, this is
// the cost of the Item.
register.enterPaidValue(20); // calling the method enterPaidValue, initiating parameter, this is
// the amount that the costumer have paid.[/COL
System.out.print("Change: ");
System.out.println(register.giveChange()); // calling giveChange method to pring out the change.
}
}
Everything is going smooth, except of the result of giveChange() method, after calling giveChange() method the result turns out to be: "Change: 0.07000000000000028", I know this is happening becuase of the behaviour of the primitive type "double", which is basicly occuring the (rounding error).
I am trying to result a better output, like "0.07" instead of "0.07000000000000028" as this will confuse the cashier, any thoughts?
Regards,
Re: Simple CashRegister program behaviour issue [Basic]
Do a google search of an article called "what every computer scientist should know about floating point arithmetic". Read it.
You shouldn't use floats (or doubles) for currency. Use an int and count the smallest unit (pennies) then figure it out from there.
You could also use a class like DecimalFormat.
Re: Simple CashRegister program behaviour issue [Basic]
Quote:
Originally Posted by
KevinWorkman
Do a google search of an article called "what every computer scientist should know about floating point arithmetic". Read it.
You shouldn't use floats (or doubles) for currency. Use an int and count the smallest unit (pennies) then figure it out from there.
You could also use a class like DecimalFormat.
I was asked to use double, thanks anyway.
PS: having a rough day? ;)
Re: Simple CashRegister program behaviour issue [Basic]
I found a very useful website that has a lot of information about formatting what you want to print out. There is specifically a section about formatting floating point numbers, which you should find very helpful :) Enjoy
A printf format reference page (cheat sheet) | printf reference for java perl ruby | devdaily.com
Re: Simple CashRegister program behaviour issue [Basic]
Quote:
Originally Posted by
Parranoia
You sir, are a life saver :), this is the kind of solution I was looking for, I edited the last line in the TestItemTester class as follows:
System.out.printf("%8.2f", register.giveChange());
output results: 0.07
Thank you very much you provided me a 2 minutes solution =D>
Re: Simple CashRegister program behaviour issue [Basic]
Quote:
Originally Posted by
Moa
I was asked to use double, thanks anyway.
PS: having a rough day? ;)
I was not having a rough day, and I was not trying to be rude. That article is required reading for every programmer.
DecimalFormat would have given you a similar 2-minute solution had you consulted the API, but at least you got a solution figured out.
Re: Simple CashRegister program behaviour issue [Basic]
Quote:
Originally Posted by
KevinWorkman
I was not having a rough day, and I was not trying to be rude. That article is required reading for every programmer.
DecimalFormat would have given you a similar 2-minute solution had you consulted the API, but at least you got a solution figured out.
I am actually giving the DecimalFormat a go too, and thanks for opening my eyes on that article, great stuff.