Format number

This example will show various ways on how to format a number in java.

Format number w/ commas

@Test
public void format_a_number_with_java () {

    double nflSalaryCap2013 = 123000000;
    
    NumberFormat formatter = NumberFormat.getIntegerInstance();
    String salaryCapFormated = formatter.format(nflSalaryCap2013);
    
    assertEquals("123,000,000", salaryCapFormated);
}

Format currency

@Test
public void format_currency_with_java () {
    
    double moMoney = 1000.01;
    NumberFormat formatter = NumberFormat.getCurrencyInstance();
    String moneyString = formatter.format(moMoney);
    
    assertEquals("$1,000.01", moneyString);
}

Format percent

@Test
public void format_a_percent_with_java () {
    NumberFormat formatter = NumberFormat.getPercentInstance();
    
    double perfectScore = 1.00;
    String perfectScoreFormatted = formatter.format(perfectScore);
    
    assertEquals("100%", perfectScoreFormatted);
}