首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在黑莓中转换双倍精度?

问如何在黑莓中转换双倍精度?
EN

Stack Overflow用户
提问于 2012-01-17 13:35:47
回答 3查看 211关注 0票数 1

我想在两个精度中转换双精度值。假设double s = 1.3333333和它的答案是这样的s = 1.33

这是使用java.text.DecimalFormat在java中完成的。

代码语言:javascript
复制
double s = 1.4454644567;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(df.format(d));

但不是在黑莓。

EN

回答 3

Stack Overflow用户

发布于 2012-01-17 15:39:02

你可以通过这段代码:

代码语言:javascript
复制
Formatter format=new Formatter();
double d1=Double.parseDouble(format.formatNumber(1.33333, 2));
double d2=Double.parseDouble(format.formatNumber(10.487586, 3));
double d3=Double.parseDouble(format.formatNumber(10.487586, 5));
System.out.println("=================: "+d1+"======"+d2+"==========="+d3);

我在控制台中的输出是:

=================:1.33======10.487===========10.48758

这里“2”、“3”和“5”表示点(.)后所需的小数位数;

票数 3
EN

Stack Overflow用户

发布于 2012-01-17 20:58:37

如果你想手工做这件事,这可能对你有帮助

代码语言:javascript
复制
    public static String roundDouble(String s, int presicion){
    int initialPos = s.indexOf('.');
    String result = s;
    String pre;
    String pos;

    if (initialPos != -1){
        pre = s.substring(0, initialPos);
        pos = s.substring(initialPos + 1, s.length());
        if (presicion < pos.length()){
            pos = s.substring(initialPos + 1, initialPos + 1 + presicion );
            int dec = Integer.parseInt(pos);
            int next = Integer.parseInt(s.substring(initialPos + 1 + presicion, initialPos + 2 + presicion )); //to round the las digit
            if (next > 4){
                dec = dec + 1;
                pos = dec + "";
                if ((dec+"").length() > presicion){
                    pre = (Integer.parseInt(pre) + 1) + "";
                    pos = "0";
                }
            }
        } else {
        }

        result = pre + "." + pos; 
    }

    return result;
}
票数 1
EN

Stack Overflow用户

发布于 2012-01-17 21:02:20

你可以使用这个

代码语言:javascript
复制
public static String roundTwoDecimal(String num) {
    // Turn random double into a clean currency format (ie 2 decimal places)
    StringBuffer result;
    double numValue = Double.parseDouble(num);
    if (Math.abs(numValue) < .01) {
        result = new StringBuffer("0.00");
    } else {
        if (numValue > 0)
            numValue = numValue + 0.005;
        else
            numValue = numValue - 0.005;

        result = new StringBuffer(Double.toString(numValue));
        final int point = result.toString().indexOf('.');
        if (point > 0) {
            // If has a decimal point, may need to clip off after 2 decimal
            // places
            if (point < (result.length() - 2)) {
                // eg "3.1415"
                result = new StringBuffer(result.toString().substring(0,
                        point + 3));
            }
        }
    }
    return result.toString();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8890164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档