我试过用NumberFormat和DecimalFormat.即使我使用的是en-In区域设置,数字也会以西方格式进行格式化.有没有选项来格式化数字格式的数字呢?
恩-我想NumberFormatInstance.format(123456)给1,23,456.00的,而不是123,456.00(例如,使用描述的系统这个维基百科页面).
由于标准化Java格式化器是不可能的,我可以提供自定义格式化程序
public static void main(String[] args) throws Exception {
System.out.println(formatLakh(123456.00));
}
private static String formatLakh(double d) {
String s = String.format(Locale.UK, "%1.2f", Math.abs(d));
s = s.replaceAll("(.+)(...\\...)", "$1,$2");
while (s.matches("\\d{3,},.+")) {
s = s.replaceAll("(\\d+)(\\d{2},.+)", "$1,$2");
}
return d < 0 ? ("-" + s) : s;
}
Run Code Online (Sandbox Code Playgroud)
产量
1,23,456.00
Run Code Online (Sandbox Code Playgroud)
虽然标准Java数字格式化程序无法处理此格式,但ICU4J中的DecimalFormat类可以.
import com.ibm.icu.text.DecimalFormat;
DecimalFormat f = new DecimalFormat("#,##,##0.00");
System.out.println(f.format(1234567));
// prints 12,34,567.00
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2880 次 |
| 最近记录: |