0
votes

I'm having a hard time getting a decimal format with currency added to my String. I've created a table with columns and rows that calculates through a while, for, and do while loop. I use table as my string to show my results. which displays my results. I'm trying to get it to format to ("$0.00"); for the whole table.

Any suggestions? I'm very new at coding so I apologize if this isn't enough information.

String result = String.format("$0.00");
System.out.println(table);

An example of my Do While Loop.

int col;  
int row = 0;
double tempRate = startingRate;       
double header;                    
double sideHeader;       
table = "";

for (header = 0; header < tableSize; header++) {

  table += " " + startingRate;
  startingRate += 0.05;

}

startingRate = tempRate;

table += "\n";
do {
  col = 0;

  startingRate = tempRate;

  sideHeader = 0;
  table += startingAmount + " ";
  do {

    table=table + (startingAmount -(startingRate * startingAmount)) + " ";
    startingRate+= .05;

    col++;

  } while (col < tableSize);

  startingAmount+= 0.50;

  row++;
  table+= "\n";

} while (row < tableSize);

sideHeader++;
1
maybe tag this with the relevant language. Is it Java? Also not understanding the question. - Scary Wombat
Yeah I'm sorry this is Java. I've got a table that I've created that calculates a discount rate and cost amount of items. It's been created with rows and columns. My table is set as the String which displays my results. I'm trying to format the table to display with a decimal format of 0.00 so theres only two decimal places - user2905208
are we talking about a HTML table or Swing or something? - Scary Wombat
Re-edited to add some example code. It's not exacutally how it should appear as how I have it on notepad++ because I'm relatively new to this website. - user2905208
NumberFormat.getCurrencyInstance() is what you want. - meghamind

1 Answers

0
votes

so you want to format a double as currency

try using this

double money = 100.1;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String moneyString = formatter.format(money);
System.out.println(moneyString);