/*
 * MosP - Mind Open Source Project    http://www.mosp.jp/
 * Copyright (C) MIND Co., Ltd.       http://www.e-mind.co.jp/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package jp.mosp.common.utils;

import java.text.NumberFormat;

import jp.mosp.common.common.ValidateUtility;

/**
 * 数字に関するPartクラス
 * @author yoshida
 * 	@version	0.0.1
 */
public class NumberUtil {
	
	/**
	 * 
	 */
	private NumberUtil() {
		// 他クラスによるインスタンス化を防止
	}
	

	/**
	 * 零
	 */
	public static final String	NAM_ZERO			= "零";
	/**
	 * 一
	 */
	public static final String	NAM_ONE				= "一";
	/**
	 * 二
	 */
	public static final String	NAM_TWO				= "二";
	/**
	 * 三
	 */
	public static final String	NAM_THREE			= "三";
	/**
	 * 四
	 */
	public static final String	NAM_FOUR			= "四";
	/**
	 * 五
	 */
	public static final String	NAM_FIVE			= "五";
	/**
	 * 六
	 */
	public static final String	NAM_SIX				= "六";
	/**
	 * 七
	 */
	public static final String	NAM_SEVEN			= "七";
	/**
	 * 八
	 */
	public static final String	NAM_EIGHT			= "八";
	/**
	 * 九
	 */
	public static final String	NAM_NINE			= "九";
	/**
	 * 十
	 */
	public static final String	NAM_TEN				= "十";
	/**
	 * 百
	 */
	public static final String	NAM_HUNDRED			= "百";
	/**
	 * 千
	 */
	public static final String	NAM_THOUSAND		= "千";
	/**
	 * 万
	 */
	public static final String	NAM_TEN_THOUSAND	= "万";
	/**
	 * 億
	 */
	public static final String	NAM_HUNDRED_MILLION	= "億";
	/**
	 * 兆
	 */
	public static final String	NAM_TRILLION		= "兆";
	

	/**
	 * @return 漢数字名称の配列
	 */
	public static final String[] getKanjiNumberArray() {
		return new String[]{ NAM_ZERO, NAM_ONE, NAM_TWO, NAM_THREE, NAM_FOUR, NAM_FIVE, NAM_SIX, NAM_SEVEN, NAM_EIGHT,
			NAM_NINE, NAM_TEN, NAM_HUNDRED, NAM_THOUSAND, NAM_TEN_THOUSAND, NAM_HUNDRED_MILLION, NAM_TRILLION };
	}
	
	/**
	 * 数字フォーマット
	 * @param number 数字
	 * @param integer 整数最大桁数
	 * @param fraction 小数点以下最大桁数
	 * @return 時間(文字列)
	 */
	public static String getNumberString(double number, int integer, int fraction) {
		NumberFormat nf = NumberFormat.getNumberInstance();
		nf.setMaximumIntegerDigits(integer);
		nf.setMaximumFractionDigits(fraction);
		return nf.format(number);
	}
	
	/**
	 * 整数変換
	 * @param input		変換前数字文字列
	 * @return	変換後整数
	 */
	public static int getIntParam(String input) {
		int num = 0;
		if (StringUtil.isNotNull(input)) {
			if (ValidateUtility.chkRegex("^-?\\d+", input)) {
				try {
					num = Integer.parseInt(input);
				} catch (NumberFormatException e) {
					num = 0;
				}
			}
		}
		return num;
	}
	
	/**
	 * 浮動小数点変換
	 * @param input		変換前数字文字列
	 * @return	変換後浮動小数点
	 */
	public static double getDoubleParam(String input) {
		double dnum = 0;
		if (StringUtil.isNotNull(input)) {
			try {
				dnum = Double.parseDouble(input);
			} catch (NumberFormatException e) {
				dnum = 0;
			}
		}
		return dnum;
	}
	
	/**
	 * 丸め処理
	 * @param input 入力値
	 * @param unit 単位
	 * @return 丸め処理後整数
	 */
	public static int roundIntegerNumber(int input, int unit) {
		int output = input;
		int round = output % unit;
		if (round > 0) {
			output += unit;
		}
		output -= round;
		return output;
	}
	
}
