문자열이나 숫자를 대문자 화폐로 바꾸다

2151 단어 문자열

/*
 * Description:          
 */
public class MoneyConvert {
	/*
	 *                 @param moneyStr @return
	 */
	public static String convertToCapitalMoney(String moneyStr) {
		double money = 0;
		try {
			money = Double.parseDouble(moneyStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertToCapitalMoney(money);
	}

	/*
	 *                @param moneyValue @return
	 */
	public static String convertToCapitalMoney(double moneyValue) {
		double money = moneyValue + 0.005; //              
		String Result = "";
		String capitalLetter = "          ";
		String moneytaryUnit = "                      ";
		String tempCapital, tempUnit;

		int integer; //       
		int point; //       
		int tempValue; //        
		integer = (int) money;
		point = (int) (100 * (money - (float) integer));

		if (integer == 0)
			Result = "  ";
		/*
		 *          1.            2.       3.       
		 */
		for (int i = 1; integer > 0; i++) {
			tempValue = (integer % 10);
			tempCapital = capitalLetter.substring(tempValue, tempValue + 1);
			tempUnit = moneytaryUnit.substring(i + 1, i + 2);
			Result = tempCapital + tempUnit + Result;
			integer = integer / 10;
		}
		/*
		 *         
		 */
		tempValue = (point / 10);
		for (int i = 1; i > -1; i--) {
			tempCapital = capitalLetter.substring(tempValue, tempValue + 1);
			tempUnit = moneytaryUnit.substring(i, i + 1);
			Result = Result + tempCapital + tempUnit;
			tempValue = point % 10;
		}
		return Result;
	}

	//    
	public static void main(String[] args) {

		String money1 = MoneyConvert.convertToCapitalMoney("33333.214");
		System.out.println(money1);
		String money = MoneyConvert.convertToCapitalMoney(33333.214);
		System.out.println(money);
	}

}

좋은 웹페이지 즐겨찾기