zoukankan      html  css  js  c++  java
  • (后台)Java:对double值进行四舍五入,保留两位小数的几种方法

    mport java.text.DecimalFormat;  
    DecimalFormat    df   = new DecimalFormat("######0.00");   
    
    double d1 = 3.23456  
    double d2 = 0.0;
    double d3 = 2.0;
    df.format(d1); 
    df.format(d2); 
    df.format(d3); 

    3个结果分别为: 

    3.23
    0.00 
    2.00

    java保留两位小数问题:

    方式一:

    四舍五入  

    double   f   =   111231.5585;  
    BigDecimal   b   =   new   BigDecimal(f);  
    double   f1   =   b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  

    保留两位小数 

    方式二:

    java.text.DecimalFormat   df   =new   java.text.DecimalFormat("#.00");  
    df.format(你要格式化的数字);

    例:

    new java.text.DecimalFormat("#.00").format(3.1415926)

    #.00 表示两位小数 #.0000四位小数 以此类推...

    方式三:

    double d = 3.1415926;
    String result = String .format("%.2f");

    %.2f %. 表示 小数点前任意位数   2 表示两位小数 格式后的结果为f 表示浮点型

    NumberFormat ddf1=NumberFormat.getNumberInstance() ;
    void setMaximumFractionDigits(int digits) 

    digits 显示的数字位数 
    为格式化对象设定小数点后的显示的最多位,显示的最后位是舍入的

    import java.text.* ; 
    import java.math.* ; 
    class TT 
    { 
    public static void main(String args[]) 
    { double x=23.5455; 
    NumberFormat ddf1=NumberFormat.getNumberInstance() ;
    
    ddf1.setMaximumFractionDigits(2); 
    String s= ddf1.format(x) ; 
    System.out.print(s); 
    } 
    }
    import java.text.*;
    DecimalFormat df=new DecimalFormat(".##");
    double d=1252.2563;
    String st=df.format(d);
    System.out.println(st);

    下面是百度

    1. 功能

    将程序中的double值精确到小数点后两位。可以四舍五入,也可以直接截断。

    比如:输入12345.6789,输出可以是12345.68也可以是12345.67。至于是否需要四舍五入,可以通过参数来决定(RoundingMode.UP/RoundingMode.DOWN等参数)。

    2. 实现代码

    复制代码
    package com.clzhang.sample;
    
    import java.math.BigDecimal;
    import java.math.RoundingMode;
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    
    public class DoubleTest {
        
        /**
         * 保留两位小数,四舍五入的一个老土的方法
         * @param d
         * @return
         */
        public static double formatDouble1(double d) {
            return (double)Math.round(d*100)/100;
        }
    
    /** * The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. * @param d * @return */ public static double formatDouble2(double d) { // 旧方法,已经不再推荐使用 // BigDecimal bg = new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP);
    // 新方法,如果不需要四舍五入,可以使用RoundingMode.DOWN BigDecimal bg = new BigDecimal(d).setScale(2, RoundingMode.UP);
    return bg.doubleValue(); } /** * NumberFormat is the abstract base class for all number formats. * This class provides the interface for formatting and parsing numbers. * @param d * @return */ public static String formatDouble3(double d) { NumberFormat nf = NumberFormat.getNumberInstance();
    // 保留两位小数 nf.setMaximumFractionDigits(2);
    // 如果不需要四舍五入,可以使用RoundingMode.DOWN nf.setRoundingMode(RoundingMode.UP);
    return nf.format(d); }
    /** * 这个方法挺简单的。 * DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. * @param d * @return */ public static String formatDouble4(double d) { DecimalFormat df = new DecimalFormat("#.00");
    return df.format(d); }
    /** * 如果只是用于程序中的格式化数值然后输出,那么这个方法还是挺方便的。 * 应该是这样使用:System.out.println(String.format("%.2f", d)); * @param d * @return */ public static String formatDouble5(double d) { return String.format("%.2f", d); } public static void main(String[] args) { double d = 12345.67890; System.out.println(formatDouble1(d)); System.out.println(formatDouble2(d)); System.out.println(formatDouble3(d)); System.out.println(formatDouble4(d)); System.out.println(formatDouble5(d)); } }
    复制代码

    3. 输出

    12345.68
    12345.68
    12,345.68
    12345.68
    12345.68

    在法语环境下,除了前两种方法显示正常之外,后边三种方法会将小数点显示成逗号,如果做国际化要注意

  • 相关阅读:
    Chandy-Lamport_algorithm
    3 differences between Savepoints and Checkpoints in Apache Flink
    列数 行数 表数 限制
    数据收集、传输、元数据管理、作业流调度、海量数据查询引擎、数据可视化
    分析云负载均衡产品
    端口被占用通过域名的处理 把www.domain.com均衡到本机不同的端口 反向代理 隐藏端口 Nginx做非80端口转发 搭建nginx反向代理用做内网域名转发 location 规则
    JSON Web Token
    查看开启端口的应用
    If the parts of an organization (e.g., teams, departments, or subdivisions) do not closely reflect the essential parts of the product, or if the relationship between organizations do not reflect the r
    微服务架构的理论基础
  • 原文地址:https://www.cnblogs.com/historylyt/p/7736560.html
Copyright © 2011-2022 走看看