我需要使用 J2ME 中的模式格式化十进制数字,就像 java DecimalFormat一样。
我看到了移植Format / NumberFormat / DecimalFormat的选项。
你能建议准备使用实现或库吗?
谢谢!
我需要使用 J2ME 中的模式格式化十进制数字,就像 java DecimalFormat一样。
我看到了移植Format / NumberFormat / DecimalFormat的选项。
你能建议准备使用实现或库吗?
谢谢!
所以,我已经设法移植桌面 java 实现:bbnumberformat:
String[] patterns = new String[] { "#,#00.00#", "0.0;(0.0)",
"0.###E0" };
DecimalFormat format = (DecimalFormat) DecimalFormat
.getNumberInstance();
double value = -12.321;
for (int i = 0; i < patterns.length; i++) {
String pattern = patterns[i];
format.applyPattern(pattern);
String text = "Pattern: " + pattern
+ " Sample: "+format.format(value)+"\n";
add(new LabelField(text));
}
下面的代码是显示如何将 double 舍入为 n 小数的方法:
private double round(double num,int numDecim){
long p=1;
//next line – calculate pow(10,brDecim)
for(int i=0; i<numDecim; i++)p*=10;
return (double)(int)(p * num + 0.5) / p;
}
您可以使用 doublep=1.0;
或p*=10.0;
代替。