4

我需要使用 J2ME 中的模式格式化十进制数字,就像 java DecimalFormat一样。

我看到了移植Format / NumberFormat / DecimalFormat的选项。

你能建议准备使用实现或库吗?

谢谢!

4

2 回答 2

5

所以,我已经设法移植桌面 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));
    }

替代文字 http://img220.imageshack.us/img220/6245/9000.jpg

于 2010-02-24T14:42:24.793 回答
2

下面的代码是显示如何将 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;代替。

于 2011-05-31T04:51:18.650 回答