Skip to content

Instantly share code, notes, and snippets.

@zhangw
Created December 28, 2017 12:57
Show Gist options
  • Select an option

  • Save zhangw/1624db864b7e8a0c4989dbe70c65b417 to your computer and use it in GitHub Desktop.

Select an option

Save zhangw/1624db864b7e8a0c4989dbe70c65b417 to your computer and use it in GitHub Desktop.
字符串金额-i18n
private static String convertStringNumber(String stringNumber, Locale locale){
BigDecimal dec = new BigDecimal(stringNumber);
int scale = dec.scale();
String format;
switch (scale) {
case 0: format = ",##0"; break;
case 1: format = ",##0.0"; break;
case 2: format = ",##0.00"; break;
case 3: format = ",##0.000"; break;
case 4: format = ",##0.0000"; break;
default:
StringBuffer sb = new StringBuffer(",##0.");
for (int i = 0; i < scale; i++) {
sb.append('0');
}
format = sb.toString();
}
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(locale);
df.applyPattern(format);
return df.format(dec);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment