• Java保留两位小数的几种做法


    1. 

    String类型数字始终保留两位小数

    public static void main(String[] args) {    
        DecimalFormat format = new DecimalFormat("0.00");
        String abc ="100.456";
        String a = format.format(new BigDecimal(abc));
        System.out.println(a);
        }

     2. 另外几种办法

    原文 

    在平时做项目时,可能有这样的业务需求:页面或界面上展示的数据保留小数点后两位。 

    为了达到这样的展示效果,本文列举了几个方法: 

    1. 使用java.math.BigDecimal 
    2. 使用java.text.DecimalFormat 
    3. 使用java.text.NumberFormat 
    4. 使用java.util.Formatter 
    5. 使用String.format
     

    ... ... 另外可以自己实现或者借用封装好的类库来实现,在这篇文章中就不一一列举了。 

    本文给出上述5种方法的简单实现。 

    代码如下: 

    Java代码  收藏代码
    1. import java.math.BigDecimal;  
    2. import java.math.RoundingMode;  
    3. import java.text.DecimalFormat;  
    4. import java.text.NumberFormat;  
    5. import java.util.Formatter;  
    6.   
    7. public final class PrecisionTest {  
    8.   
    9.     private PrecisionTest() {  
    10.     }  
    11.   
    12.     /** 
    13.      * 使用BigDecimal,保留小数点后两位 
    14.      */  
    15.     public static String format1(double value) {  
    16.   
    17.         BigDecimal bd = new BigDecimal(value);  
    18.         bd = bd.setScale(2, RoundingMode.HALF_UP);  
    19.         return bd.toString();  
    20.     }  
    21.   
    22.     /** 
    23.      * 使用DecimalFormat,保留小数点后两位 
    24.      */  
    25.     public static String format2(double value) {  
    26.   
    27.         DecimalFormat df = new DecimalFormat("0.00");  
    28.         df.setRoundingMode(RoundingMode.HALF_UP);  
    29.         return df.format(value);  
    30.     }  
    31.   
    32.     /** 
    33.      * 使用NumberFormat,保留小数点后两位 
    34.      */  
    35.     public static String format3(double value) {  
    36.   
    37.         NumberFormat nf = NumberFormat.getNumberInstance();  
    38.         nf.setMaximumFractionDigits(2);  
    39.         /* 
    40.          * setMinimumFractionDigits设置成2 
    41.          *  
    42.          * 如果不这么做,那么当value的值是100.00的时候返回100 
    43.          *  
    44.          * 而不是100.00 
    45.          */  
    46.         nf.setMinimumFractionDigits(2);  
    47.         nf.setRoundingMode(RoundingMode.HALF_UP);  
    48.         /* 
    49.          * 如果想输出的格式用逗号隔开,可以设置成true 
    50.          */  
    51.         nf.setGroupingUsed(false);  
    52.         return nf.format(value);  
    53.     }  
    54.   
    55.     /** 
    56.      * 使用java.util.Formatter,保留小数点后两位 
    57.      */  
    58.     public static String format4(double value) {  
    59.         /* 
    60.          * %.2f % 表示 小数点前任意位数 2 表示两位小数 格式后的结果为 f 表示浮点型 
    61.          */  
    62.         return new Formatter().format("%.2f", value).toString();  
    63.     }  
    64.   
    65.     /** 
    66.      * 使用String.format来实现。 
    67.      *  
    68.      * 这个方法其实和format4是一样的 
    69.      */  
    70.     public static String format5(double value) {  
    71.   
    72.         return String.format("%.2f", value).toString();  
    73.     }  
    74. }  


    测试代码及结果: 

    Java代码  收藏代码
    1. public class Main {  
    2.   
    3.     public static void main(String[] args) {  
    4.   
    5.         double[] testData = new double[] { 100.123D, 1234567.897D, 100.0052D,  
    6.                 80.00D };  
    7.   
    8.         for (double value : testData) {  
    9.             System.out.println(PrecisionTest.format1(value));  
    10.             System.out.println(PrecisionTest.format2(value));  
    11.             System.out.println(PrecisionTest.format3(value));  
    12.             System.out.println(PrecisionTest.format4(value));  
    13.             System.out.println(PrecisionTest.format5(value));  
    14.         }  
    15.     }  
    16. }  


    100.12 
    100.12 
    100.12 
    100.12 
    100.12 
    1234567.90 
    1234567.90 
    1234567.90 
    1234567.90 
    1234567.90 
    100.01 
    100.01 
    100.01 
    100.01 
    100.01 
    80.00 
    80.00 
    80.00 
    80.00 
    80.00
  • 相关阅读:
    运行python脚本后台执行(转载)
    双积分式(A/D)转换器电路结构及工作原理
    SAR ADC简介
    [hdu-6395]Sequence 分块+矩阵快速幂
    [hdu-1272]小希的迷宫 并查集
    redhat7.2安全基线BI
    Oracle 12c RAC
    .gitlab-ci.yml简介
    gitlab
    Centos 7 Docker安装配置
  • 原文地址:https://www.cnblogs.com/wxmdevelop/p/5412003.html
Copyright © 2020-2023  润新知