0

Hello my question is about double formatting within Stringbuilder.

StringBuilder autoSB = new StringBuilder();

  autoSB.append( super.toString( ) )
    .append( "\nI own a ")
    .append( getMY( ) )
    .append( " ")
    .append( getMM( ) )
    .append( ", My VIN is ")
    .append( getVin())
    .append( ". I paid $")
    .append( getAmount( ) )
    .append( " with a deductible of $")
    .append( getDeductible() )
    .append( ".00.\n");

Output:

I own Policy#: AS14325, insuring Sue Bates, with a premium of $300.00. I own a 2001 Ford Focus, My VIN is N1029345G9V1249P4. I paid $11000, with a deductible of $1000.00.

Wanted Output:

I own Policy#: AS14325, insuring Sue Bates, with a premium of $300.00. I own a 2001 Ford Focus, My VIN is N1029345G9V1249P4. I paid $11,000.00, with a deductible of $1,000.00.

How on earth do I format doubles in stringbuilder?

V/R

Gill Bates

  • Use `NumberFormat` for this. `StringBuilder` just create string from chunks, nothing more – Antoniossss Apr 20 '18 at 17:54
  • I think you could use `MessageFormat` along with a format string which would allow you to do all of this – ifly6 Apr 20 '18 at 18:36
  • Thanks for the help! I ended up using: import java.text.NumberFormat; public String toString ( ) { NumberFormat form = NumberFormat.getCurrencyInstance( ); autoSB.append(form.format(...( ) ) ); } – Gill Bates Apr 22 '18 at 14:46

0 Answers0