zoukankan      html  css  js  c++  java
  • 数字格式化工具

    package com.stylefeng.guns.core.util;
    
    import java.math.BigDecimal;
    import java.math.RoundingMode;
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    
    /**
     * 数字格式化的类
     *
     */
    public class NumUtil {
    
        /**
         * @Description 保留指定位数的小数(少的位数不补零)
         **/
        public static String keepRandomPoint(Double value, int n) {
            if (value == null) {
                value = 0.00;
                return new BigDecimal(value).setScale(n, RoundingMode.HALF_UP).toString();
            } else {
                return new BigDecimal(value).setScale(n, RoundingMode.HALF_UP).toString();
            }
        }
    
        /**
         * @Description 浮点保留两位小数(少的位数不补零)
         * */
        public static String keep2Point(double value) {
            return keepRandomPoint(value, 2);
        }
    
        /**
         * @Description 浮点保留1位小数(少的位数不补零)
         * */
        public static String keep1Point(double value) {
            return keepRandomPoint(value, 1);
        }
    
        /**
         * @Description 浮点保留任意位小数(少位补零)
         * */
        public static String keepRandomPointZero(double value, int n) {
            DecimalFormat df = new DecimalFormat("#0.00");
            return df.format(Double.valueOf(keepRandomPoint(value, n)));
        }
    
        /**
         * @Description 浮点保留两位小数(少位补零)
         * */
        public static String keep2PointZero(double value) {
            return keepRandomPointZero(value, 2);
        }
    
        /**
         * @Description 获取任意小数点位的百分比表示
         **/
        public static String percentRandomPoint(double value, int n) {
            NumberFormat percent = NumberFormat.getPercentInstance();
            percent.setGroupingUsed(false);
            percent.setMaximumFractionDigits(n);
            return percent.format(value);
        }
    
        /**
         * @Description 百分比保留两位小数
         * */
        public static String percent2Point(double value) {
            return percentRandomPoint(value, 2);
        }
    
        /**
         * @Description 获取格式化经纬度后的小数(保留3位)
         * */
        public static String latLngPoint(double value) {
            return keepRandomPoint(value, 3);
        }
    
    }
  • 相关阅读:
    HDU 3911 Black And White 分段树 题解
    Haskell 差点儿无痛苦上手指南
    CFileDialog的使用方法简单介绍
    对 dpif_class 结构体的一点认识
    三层架构之基础知识
    五类常见算法小记 (递归与分治,动态规划,贪心,回溯,分支界限法)
    AlertDialog具体解释
    delphi tcp/ip IdTCPServer1实例一
    23种设计模式(15):备忘录模式
    Android APK反编译具体解释(附图)
  • 原文地址:https://www.cnblogs.com/JonaLin/p/11248273.html
Copyright © 2011-2022 走看看