• 保留两位小数


    public static void main(String[] args) {  
           String str="86.64466666";  
           BigDecimal bd = new BigDecimal(Double.parseDouble(str));  
           System.out.println(bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());  
           System.out.println("=================");  
             DecimalFormat df = new DecimalFormat("#.00");   
             System.out.println(df.format(Double.parseDouble(str)));   
             System.out.println("=================");  
             System.out.println(String.format("%.2f", Double.parseDouble(str)));  
             System.out.println("=================");  
             NumberFormat nf = NumberFormat.getNumberInstance();   
             nf.setMaximumFractionDigits(2);   
             System.out.println(nf.format(Double.parseDouble(str)));   
       }  


    基本数据类型转String的时候可以通过包装类进行自动拆箱

  • 相关阅读:
    Django第一天
    约束条件 表之间的关系
    数据类型
    初始vue
    JQ事件和事件对象
    Jquery的属性操作和DOM操作
    浏览器对象BOM
    Swiper实现全屏视觉差轮播
    Swiper开篇
    JSON
  • 原文地址:https://www.cnblogs.com/cfb513142804/p/5315717.html
Copyright © 2020-2023  润新知