【问题标题】:Show Normal Number Instead of Exponential Form显示普通数而不是指数形式
【发布时间】:2011-04-04 07:29:07
【问题描述】:

我想以普通形式而不是指数形式显示数字

例如,我的号码存储在值为 1234567890123 的双精度变量中

我想得到确切的表示。但是当我将它传递给一些 TextView 进行显示时,它变成了 1.234E12

【问题讨论】:

    标签: android double normalization bigdecimal primitive-types


    【解决方案1】:

    在 java 中尝试使用 Big decimal 类..

    大十进制类具有一些内置的舍入函数的优势,您可以使用这些函数,例如:

    Double a = 7.77d * 100000000; //Multiply Operation
    
    System.out.println("Large multiply " + a.doubleValue());
    
    System.out.println("Magic of big decimal " + BigDecimal.valueOf(a).setScale(0,RoundingMode.HALF_EVEN).toPlainString());
    
    a = 7.77d / 100000; //Devide Operation
    
    System.out.println("Devide operation " + a.doubleValue());
    
    System.out.println("Magic of big decimal " + BigDecimal.valueOf(a).toPlainString());
    
    DecimalFormat formatter = new DecimalFormat("0.000000");
    
    System.out.println("Trimming the big string : "+formatter .format(a)); 
    

    输出:

    Large multiply 7.77E8
    
    Magic of big decimal 777000000
    
    Devide operation 7.769999999999999E-5
    
    Magic of big decimal 0.00007769999999999999
    
    Trimming the big string : 0.000078
    

    【讨论】:

    • 您提供的链接已失效。
    【解决方案2】:

    你可以试试这个:

    double v = 1234567890123d;
    Double d = new Double( v );
    

    现在当你将它传递给 TextView 时,你可以按如下方式传递它(假设你只对 double 的整数部分感兴趣):

    d.longValue();
    

    现在,为什么它给你 1.234E12: 打印双打的规则可以在here 找到(参见toString 部分)。它准确描述了打印时数字何时切换为科学记数法。

    您也可以查看NumberFormat

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 2019-09-02
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多