-
June 22nd, 2011, 10:26 PM
#1
Formating Double as a Decimal
You guys have been a lot of help so far. I've had to create a program that has an array of 5 items, where one side is an item, the other is a price of that item. I've created everything, but I'm struggling to format the price as a decimal. I have 2 classes written. I am posting the class that has my variables assigned. I think that's where I need to do it. I successfully did this last week with an array where the user entered 5 values, but as each array has 2 values, the method I learned last week formatter.format(x); isn't working. printf wouldn't work either. Anyways, here is my code. I need Price to have a set 2 decimal places on output.
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Item
{
//private instance variables
private String Name;
private double price;
//Create object to format as decimal
NumberFormat formatter = new DecimalFormat("#0.00");
public String getName()
{
return this.Name;
}
public void setItemName(String Name)
{
this.Name = Name;
}
public double getPrice()
{
return this.price;
}
public void setPrice(double Name)
{
this.price = price;
}
public String toString()
{
return this.getName() + " $" + this.getPrice();
}
//constructor
public Item(String Name, double price)
{
this.Name = Name;
this.price = price;
}
}
My array looks like: ItemArray[0] = new Item("Donut", 1.50);
I'm thinknig maybe instead of a double for price I should use a string, but that is only a work around and not a real solution. It'd work for this program, but it doesn't teach me anything.
-
June 23rd, 2011, 06:42 AM
#2
Re: Formating Double as a Decimal
I need Price to have a set 2 decimal places on output
....
formatter.format(x); isn't working.
Please show the output and desribe what is wrong with it and how you would like it to look
Norm
-
June 23rd, 2011, 06:53 AM
#3
Re: Formating Double as a Decimal
Originally Posted by jbrintnell
... the method I learned last week formatter.format(x); isn't working. printf wouldn't work either.
The code you posted isn't trying to format the output at all, so it's not surprising it doesn't work properly.
If you post up the code you tried to do the formatting, we might be able to suggest where it's going wrong.
Having said that, when I use formatter.format(this.getPrice()) in the toString() method of your Item class, toString() returns a String like this:
Donut $1.50
This seems to be what you wanted, so I can't really see what the problem is.
Testing can show the presence of errors, but not their absence...
E. Dijkstra
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
June 23rd, 2011, 09:41 AM
#4
Re: Formating Double as a Decimal
Thank you, I put my formatter line in the wrong area. That fixed it.
I had my formatter in the constructor for price. It works as intended this way.
Thank you
-
June 24th, 2011, 04:37 AM
#5
Re: Formating Double as a Decimal
Originally Posted by jbrintnell
I had my formatter in the constructor for price.
?? price is a double, it doesn't have a constructor.
It works as intended this way.
You haven't posted the new code, so I'll have to take your word for it.
It is not knowledge, but the act of learning, not possession, but the act of getting there which generates the greatest satisfaction...
F. Gauss
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|