J2ME-如何在不使用10表示倍数的情况下将double转换为字符串(E-05) [英] J2ME - How to convert double to string without the power to 10 representation (E-05)

查看:55
本文介绍了J2ME-如何在不使用10表示倍数的情况下将double转换为字符串(E-05)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有双重价值.我想将其存储在不带E表示法的String中(在J2ME中)

I have a double value . I want to store it in String with out E notation (in J2ME)

示例

双值6.887578324E9 想要显示为6887578342

Double value 6.887578324E9 Want to show as 6887578342

推荐答案

您可以创建自己的方法来执行此操作,或者使用一些已经存在的库. 例如,Javolution为您提供了类和方法 TypeFormat.format(双精度d,整数,布尔科学,布尔showZero,附加a) 检查 Javolution ,它有很多不错的实用程序,但是如果您唯一需要的就是要格式化数字,只需编写您自己的方法. 这是大量数字的快速破解方法

You can create your own method to do that, or use some already existing library. Javolution, for example, gives you the class and method TypeFormat.format(double d, int digits, boolean scientific, boolean showZero, Appendable a) Check Javolution, it has lots of nice utilities, but if the only thing you need is to format numbers, just write your own method. Here is a quick hack for big numbers

    private static String nosci(double d) {
    if(d < 0){
        return "-" + nosci(-d);
    }
    String javaString = String.valueOf(d);
    int indexOfE =javaString.indexOf("E"); 
    if(indexOfE == -1){
        return javaString;
    }
    StringBuffer sb = new StringBuffer();
    if(d > 1){//big number
        int exp = Integer.parseInt(javaString.substring(indexOfE + 1));
        String sciDecimal = javaString.substring(2, indexOfE);
        int sciDecimalLength = sciDecimal.length();
        if(exp == sciDecimalLength){
            sb.append(javaString.charAt(0));
            sb.append(sciDecimal);              
        }else if(exp > sciDecimalLength){
            sb.append(javaString.charAt(0));
            sb.append(sciDecimal);
            for(int i = 0; i < exp - sciDecimalLength; i++){
                sb.append('0');
            }
        }else if(exp < sciDecimalLength){
            sb.append(javaString.charAt(0));
            sb.append(sciDecimal.substring(0, exp));
            sb.append('.');
            for(int i = exp; i < sciDecimalLength ; i++){
                sb.append(sciDecimal.charAt(i));
            }
        }
      return sb.toString();
    }else{
        //for little numbers use the default or you will
        //loose accuracy
        return javaString;
    }       


}

这篇关于J2ME-如何在不使用10表示倍数的情况下将double转换为字符串(E-05)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆