Last active
November 2, 2020 09:10
-
-
Save jaizon2000/6ac9f5510f9fdcac40441a632ebbacdd to your computer and use it in GitHub Desktop.
How to use NumberFormat (Java)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Result