Skip to content

Instantly share code, notes, and snippets.

@jaizon2000
Last active November 2, 2020 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaizon2000/6ac9f5510f9fdcac40441a632ebbacdd to your computer and use it in GitHub Desktop.
Save jaizon2000/6ac9f5510f9fdcac40441a632ebbacdd to your computer and use it in GitHub Desktop.
How to use NumberFormat (Java)
public class NumberFormatExample {
public String getStatsOf(List<Items> list) {
// StringBuilder not required, but makes it easier to read
StringBuilder result = new StringBuilder();
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
// .setMaximumFractionDigits -- sets how many decimal places it should put
currencyFormat.setMaximumFractionDigits(0);
result.append("Statistics of Items\n\n");
if (propertyList.size() > 0) {
result.append("n =" + list.size() + "\n");
result.append("min = " + currencyFormat.format(1) + "\n");
result.append("max = " + currencyFormat.format(40)) + "\n");
result.append("range = " + currencyFormat.format(10) + "\n");
result.append("mean = " + currencyFormat.format(5) + "\n");
result.append("sd = " + currencyFormat.format(6) + "\n");
result.append("median = " + currencyFormat.format(7) + "\n");
}
return result.toString();
}
}
@jaizon2000
Copy link
Author

Example Result

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment