So I am at somewhat of a loss here. I am trying to format my strings to be only 2 decimal places. I know what you are thinking and I have to have them as strings in order to append them to my JTextArea (these are a small part of my arrayList). So I have converted the doubles into strings using String.valueof() which works fine, however I need to format them in order to print out the string with 2 decimal places. My delcarations and such are as follows
class inventoryItem { //Delcare variables below
String itemName;
String newUnit;
String newFee;
String newValue;
String newInventoryValue;
int itemNumber;
int inStock;
double unitPrice;
double value;
double restockingFee;
double inventoryValue;
public inventoryItem(String itemName, int itemNumber, int inStock, double unitPrice) { //declare variables for this class
this.itemName = itemName;
this.itemNumber = itemNumber;
this.inStock = inStock;
this.unitPrice = unitPrice;
restockingFee = .05 * value;
stockValue();
}
public void stockValue() {
value = unitPrice * inStock;
restockingFee = .05 * unitPrice;
inventoryValue = restockingFee + value;
newUnit = String.valueOf((double) unitPrice);
newFee = String.valueOf((double) restockingFee);
newValue = String.valueOf((double) value);
newInventoryValue = String.valueOf(inventoryValue);
I have tried to do something like
outputText.append("something = $" + (String.format("%.2f, newUnit))
but that doesn't seem to work. Is there a way that I can do it while using String.valueOf?