Java tutorial
/* * 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.sunchenbin.store.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.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.sunchenbin.store.feilong.core.lang.NumberPattern; /** * {@link NumberFormat}?,?????. * * <p> * ? {@link ChoiceFormat}, {@link DecimalFormat}.<br> * ?:<span style="color:red">{@link DecimalFormat}?? </span>,??. (?JAVA API ) * </p> * * @author feilong * @version 1.0.2 2012-3-27 ?1:39:53 * @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> * {@link java.math.RoundingMode#HALF_UP} * </p> * * @param value * the value * @param numberPattern * the pattern {@link NumberPattern} * @return null * @see NumberPattern * @see DecimalFormat * @see RoundingMode#HALF_UP */ public static String format(Number value, String numberPattern) { // ?, DecimalFormat RoundingMode.HALF_EVEN RoundingMode roundingMode = RoundingMode.HALF_UP; return format(value, numberPattern, roundingMode); } /** * {@link Number} {@link RoundingMode} numberPattern?. * * @param value * the value * @param numberPattern * the pattern {@link NumberPattern} * @param roundingMode * ?{@link RoundingMode} * @return null * @see NumberPattern * @see DecimalFormat * @see <a href="../util/NumberUtil.html#RoundingMode">JAVA 8??</a> */ public static String format(Number value, String numberPattern, RoundingMode roundingMode) { if (null == value) { throw new NullPointerException("the value is null or empty!"); } if (null == numberPattern) { throw new NullPointerException("the numberPattern is null or empty!"); } try { // 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 format = decimalFormat.format(value); if (LOGGER.isDebugEnabled()) { LOGGER.debug("value:[{}], pattern:[{}],return:[{}],decimalFormat.toLocalizedPattern():[{}]", value, numberPattern, format, decimalFormat.toLocalizedPattern()//?? Format ????. ); } return format; } catch (Exception e) { Object[] objects = { e.getMessage(), value, numberPattern }; LOGGER.error("{},value:[{}],pattern:[{}]", objects); LOGGER.error(e.getClass().getName(), e); } return StringUtils.EMPTY; } }