/*
 * MosP - Mind Open Source Project         http://www.mosp.jp/
 * Copyright (C) 1987-2009 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.part;

import java.text.NumberFormat;

import jp.mosp.common.common.ValidateUtility;

/**
 * 数字に関するPartクラス
 * @author yoshida
 * 	@version	0.0.1
 */
public class NumberPart {

	/**
	 * 数字フォーマット
	 * @param number 数字
	 * @param max 整数最大桁数
	 * @param min 小数点以下最大桁数
	 * @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 (StringPart.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 (StringPart.isNotNull(input)) {
			try {
				dnum = Double.parseDouble(input);
			} catch (NumberFormatException e) {
				dnum = 0;
			}
		}
		return dnum;
	}

}
