[Java]常用數字函數

字串取小數幾位,不足碼補零

package com.yslifes.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

public class NumberUtils {
    /**
     * 字串取小數幾位
     * 
     * @param str
     *            字串(數字)
     * @param precision
     *            小數幾碼
     * @return 取好的字串
     */
    public static String MarkUpZero(String str, int precision) {
        String maskup = "00000000000";
        String retVal = str;

        retVal = StringUtils.nvl(retVal, "0");

        if (precision == 0) {
            int idx = retVal.indexOf(".");
            if (idx == -1)
                return retVal;
            else
                return retVal.substring(0, idx);
        }
        if (precision > 0 && retVal.indexOf(".") == -1) {
            retVal += ".";
        }
        int len = retVal.indexOf(".");

        return (retVal + maskup).substring(0, retVal.indexOf("."))
                + (retVal + maskup).substring(retVal.indexOf("."), len
                        + precision + 1);

    }
    public static void main(String args[]) {
        System.out.println("22222取小數二位:\t\t\t"+MarkUpZero("22222", 2));
        System.out.println("22222.1取小數二位:\t\t"+MarkUpZero("22222.1", 2));
        System.out.println("22222.11111取小數二位:\t\t"+MarkUpZero("22222.11111", 2));
        System.out.println("22222.1取小數零位:\t\t"+MarkUpZero("22222.1", 0));
        
        
    }
}

數字自動補comma ,

package com.yslifes.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

public class NumberUtils {
    /**
     * 數字加上,
     * 
     * @param data
     *            要加comma的數字
     * @return comma的字串
     */
    public static String InserComma(BigDecimal data) {
        if (data == null) {
            return "";
        } else {
            DecimalFormat df = new DecimalFormat();
            DecimalFormatSymbols dfs = new DecimalFormatSymbols();
            dfs.setGroupingSeparator(',');

            df.setDecimalFormatSymbols(dfs);
            return df.format(data);
        }
    }
    public static void main(String args[]) {
        System.out.println("1234567890="+InserComma(new BigDecimal("1234567890")));        
        
    }
}

數字整數部份補comma,並取小數n位

package com.yslifes.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

public class NumberUtils {
/**
     * 數字加上,並取得n位小數位數
     * 
     * @param data
     *            要加comma的數字
     * @param 取幾位小數
     * @return comma及取好小數位數的字串
     */
    public static String InserCommaMarkUpZero(BigDecimal data, int precision) {
        if (data == null) {
            return "";
        } else {
            DecimalFormat df = new DecimalFormat();
            DecimalFormatSymbols dfs = new DecimalFormatSymbols();
            dfs.setGroupingSeparator(',');

            df.setDecimalFormatSymbols(dfs);
            BigDecimal itg = new BigDecimal(data.longValue());
            String f = MarkUpZero(data.subtract(itg).toString(), precision);
            if (f.equals("0"))
                f = "";
            else
                f = f.replaceFirst("0\\.", ".");
            return df.format(itg) + f;
        }
    }


    public static void main(String args[]) {
        System.out.println("1234567890=\t"+InserCommaMarkUpZero(new BigDecimal("1234567890"),2));        
        System.out.println("1234567890.1=\t"+InserCommaMarkUpZero(new BigDecimal("1234567890.1"),2));
        System.out.println("1234567890.12=\t"+InserCommaMarkUpZero(new BigDecimal("1234567890.12"),2));
        System.out.println("1234567890.123=\t"+InserCommaMarkUpZero(new BigDecimal("1234567890.123"),2));
        System.out.println("1234567890.12=\t"+InserCommaMarkUpZero(new BigDecimal("1234567890.12"),0));
    }
}

BigDecimal判斷是否有小數(為整數)

BigDecimal points = new BigDecimal("200.1");
if(points.compareTo(points.setScale(0,RoundingMode.DOWN))!=0)
{
    System.out.println("不可有小數!!");
}

發表迴響