com.feilong.core.text.NumberFormatUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.core.text.NumberFormatUtil.java

Source

/*
 * Copyright (C) 2008 feilong
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.feilong.core.text;

import java.math.RoundingMode;
import java.text.ChoiceFormat;
import java.text.DecimalFormat;
import java.text.Format;
import java.text.NumberFormat;

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.core.NumberPattern;

/**
 * {@link NumberFormat}?,?????.
 * 
 * <p>
 * ?: {@link ChoiceFormat}, {@link DecimalFormat}.<br>
 * ?:<span style="color:red">{@link DecimalFormat}?? </span>,??.(?JAVA API )
 * </p>
 * 
 * @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
 * @see Format
 * @see NumberFormat
 * @see DecimalFormat
 * @see NumberPattern
 * @since 1.0.2
 */
public final class NumberFormatUtil {

    /** The Constant LOGGER. */
    private static final Logger LOGGER = LoggerFactory.getLogger(NumberFormatUtil.class);

    /** Don't let anyone instantiate this class. */
    private NumberFormatUtil() {
        //AssertionError?. ?????. ???.
        //see Effective Java 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    /**
     *  {@link Number}  numberPattern?.
     * 
     * <p style="color:red">
     * ?, {@link com.feilong.core.lang.NumberUtil#toString(Number, String) NumberUtil.toString(Number, String)}
     * </p>
     * 
     * <p>
     *  {@link java.math.RoundingMode#HALF_UP}
     * </p>
     *
     * @param value
     *            the value
     * @param numberPattern
     *            the pattern {@link NumberPattern}
     * @return  <code>value</code> null, {@link NullPointerException}<br>
     *          <code>numberPattern</code> null, {@link NullPointerException}<br>
     *          <code>numberPattern</code> blank, {@link IllegalArgumentException}<br>
     * @see NumberPattern
     * @see DecimalFormat
     * @see RoundingMode#HALF_UP
     * @see #format(Number, String, RoundingMode)
     */
    public static String format(Number value, String numberPattern) {
        RoundingMode roundingMode = RoundingMode.HALF_UP;
        return format(value, numberPattern, roundingMode);
    }

    /**
     *  {@link Number}  {@link RoundingMode} <code>numberPattern</code>?.
     *
     * @param value
     *            the value
     * @param numberPattern
     *            the pattern {@link NumberPattern}
     * @param roundingMode
     *            ?{@link RoundingMode}
     * @return  <code>value</code> null, {@link NullPointerException}<br>
     *          <code>numberPattern</code> null, {@link NullPointerException}<br>
     *          <code>numberPattern</code> blank, {@link IllegalArgumentException}<br>
     * @see DecimalFormat
     * @see <a href="../util/NumberUtil.html#RoundingMode">JAVA 8??</a>
     */
    public static String format(Number value, String numberPattern, RoundingMode roundingMode) {
        Validate.notNull(value, "value can't be null!");
        Validate.notBlank(numberPattern, "numberPattern can't be null!");

        // applyPattern(pattern, false)
        DecimalFormat decimalFormat = new DecimalFormat(numberPattern);

        // ? RoundingMode.HALF_EVEN  ?,?.?,?.??,?,??.???1?,???.
        // 1.15>1.2 1.25>1.2
        if (null != roundingMode) {
            decimalFormat.setRoundingMode(roundingMode);
        }
        String result = decimalFormat.format(value);
        LOGGER.trace("input:[{}],with:[{}]=[{}],localizedPattern:[{}]", value, numberPattern, result,
                decimalFormat.toLocalizedPattern());
        return result;
    }
}