com.ms.app.web.commons.tools.CurrencyFormattor.java Source code

Java tutorial

Introduction

Here is the source code for com.ms.app.web.commons.tools.CurrencyFormattor.java

Source

/*
 * Copyright 2011-2016 ZXC.com All right reserved. This software is the confidential and proprietary information of
 * ZXC.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with ZXC.com.
 */
package com.ms.app.web.commons.tools;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;

import org.apache.commons.lang.math.NumberUtils;

/**
 * ???
 * 
 * @author zxc Apr 12, 2013 10:59:09 PM
 */
public class CurrencyFormattor {

    private static ThreadLocal<HashMap<String, NumberFormat>> formatHolder = new ThreadLocal<HashMap<String, NumberFormat>>();
    private static final String CURRENCY = "#0.00";
    // ???
    private static final String NUMBER = "#0.0";
    private static final String PERCENT = "#.##%";

    private static final Integer SCALE = 2;

    /**
     * 
     * 
     * @param yuan ?( 10.0010)
     * @return
     */
    public static int convert2fen(String yuan) {
        return (int) (NumberUtils.toFloat(yuan) * 100);
    }

    /**
     * ????
     * 
     * <pre>
     * format(null) 0.00
     * format(?1) 0.00
     * format(0) 0.00
     * format(1) 1.00
     * format(1000000) 10000.00
     * </pre>
     * 
     * @param price ?
     * @return 
     */
    public static String format(Integer fen) {
        double yuan = 0;
        if (isNotNull(fen)) {
            yuan = fen / 100d;
        }
        NumberFormat numberInstance = getFormat(CURRENCY);
        return numberInstance.format(yuan);
    }

    /**
     * ??
     * 
     * @param data 
     * @param divide 
     * @return ???
     */

    public static String format(Integer data, Double divide) {
        double result = 0;
        result = data / divide;
        NumberFormat numberInstance = getFormat(CURRENCY);
        String strValue = numberInstance.format(result);
        // ???0
        if (strValue != null && strValue.length() > 0) {
            strValue = strValue.replaceAll("(\\.0+|0+)$", "");
        }
        return strValue;
    }

    /**
     * ?1??
     * 
     * @param data
     * @param divide
     * @return
     */
    public static String formatWith1Dot(Integer data, Double divide) {
        double result = 0;
        result = data / divide;
        NumberFormat numberInstance = getFormat(NUMBER);
        String strValue = numberInstance.format(result);
        // ???0
        if (strValue != null && strValue.length() > 0) {
            strValue = strValue.replaceAll("(\\.0+|0+)$", "");
        }
        return strValue;
    }

    /**
     * ?2??
     * 
     * @param data
     * @param divide
     * @return
     */
    public static Double formatWith2Dot(Integer data, Double divide) {
        double result = 0;
        result = data / divide;
        Long tmp = Math.round(result * 100);
        result = (tmp / 100d);
        return result;
    }

    /**
     * ????<br>
     * ,""
     * 
     * @param fen
     * @return
     */
    public static String formatShowEmpty(Integer fen) {
        double yuan = 0;
        if (isNotNull(fen)) {
            yuan = fen / 100d;
            NumberFormat numberInstance = getFormat(CURRENCY);
            return numberInstance.format(yuan);
        }
        return "";
    }

    /**
     * ????,??.00<br>
     * ,""
     * 
     * @param fen
     * @return
     */
    public static String formatFen(Integer fen) {
        int yuan = 0;
        if (isNotNull(fen)) {
            yuan = fen / 100;
            return "" + yuan;
        }
        return "";
    }

    /**
     * 
     * 
     * @param fen
     * @return
     */
    public static int yuanTofen(String yuan) {
        try {
            float fen = Float.parseFloat(yuan);
            if (fen > 0) {
                return (int) (fen * 100 + 0.001f);
            }
        } catch (Exception e) {
        }
        return 0;
    }

    /**
     * 
     * 
     * @param fen
     * @return
     */
    public static int yuanTofen(Float yuan) {
        if (yuan != null && yuan > 0) {
            // +0.001 java?
            return (int) (yuan.floatValue() * 100 + 0.001f);
        }
        return 0;
    }

    /**
     * 
     * 
     * @param fen
     * @return
     */
    public static int yuanTofen(Integer yuan) {
        if (yuan != null && yuan > 0) {
            return (int) (yuan * 100);
        }
        return 0;
    }

    private static boolean isNotNull(Integer fen) {
        return fen != null;
    }

    /**
     * ?
     * 
     * @param number
     * @return
     */
    public static String formatPercent(float number) {
        NumberFormat format = getFormat(PERCENT);
        return format.format(number);
    }

    public static float toFloat(String number) {
        try {
            return Float.parseFloat(number);
        } catch (Exception e) {
            return 0;
        }
    }

    private static NumberFormat getFormat(String key) {
        HashMap<String, NumberFormat> map = formatHolder.get();
        if (map == null) {
            map = new HashMap<String, NumberFormat>(3);
            formatHolder.set(map);// ?
        }
        NumberFormat format = map.get(key);
        if (format == null) {
            format = new DecimalFormat(key);
            map.put(key, format);
            formatHolder.set(map);// ?
        }
        return format;
    }

    /**
     * 
     */
    public static Integer formatPrice(Integer price) {
        if (price == null) {
            return null;
        }
        return price.intValue() / 100;
    }

    /**
     * "###.####"-->"#.00" floatPrice-->formatPrice(?)
     */
    public static String float2formatPrice(Float price) {
        if (price == null) {
            return null;
        }

        BigDecimal b = new BigDecimal(price);
        float f = b.setScale(SCALE, BigDecimal.ROUND_HALF_UP).floatValue();
        DecimalFormat fnum = new DecimalFormat(CURRENCY);

        return fnum.format(f);
    }

    /**
     * 100? ??
     */
    public static Float formatScore(Integer score) {
        if (score == null) {
            return null;
        }
        return score.floatValue() / 100;
    }

    /**
     * ?
     * 
     * @param str
     * @return
     */
    public static Integer strToInt(String str) {
        try {
            Float num = Float.parseFloat(str);
            return num.intValue();
        } catch (Exception e) {
            return null;
        }
    }
}