首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

金额折算(数字换汉字)

2012-07-04 
金额换算(数字换汉字)/** * 金额数据中文格式化工具类 ** @since version1.0 */public class MoneyToChine

金额换算(数字换汉字)

/** * 金额数据中文格式化工具类 *  * @since version1.0 */public class MoneyToChinese {public MoneyToChinese() {super();}private final static String HanDigiStr[] = new String[] { "零", "壹", "贰", "叁", "肆", "伍", "陆","柒", "捌", "玖" };private final static String HanDiviStr[] = new String[] { "", "拾", "佰", "仟", "万", "拾", "佰", "仟","亿", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万","拾", "佰", "仟" };/** * 将数字转为大写的汉字表示形式 *  * @param String NumStr * @return String */public static String PositiveIntegerToHanStr(String NumStr) { // 输入字符串必须正整数,只允许前置空格(必须右对齐),不宜有前置零String RMBStr = "";boolean lastzero = false;boolean hasvalue = false; // 亿、万进位前有数值标记int len, n;len = NumStr.length();if (len > 15)return "数值过大!";for (int i = len - 1; i >= 0; i--) {if (NumStr.charAt(len - i - 1) == ' ')continue;n = NumStr.charAt(len - i - 1) - '0';if (n < 0 || n > 9)return "输入含非数字字符!";if (n != 0) {if (lastzero)RMBStr += HanDigiStr[0]; // 若干零后若跟非零值,只显示一个零// 如十进位前有零也不发壹音用此行if (!(n == 1 && (i % 4) == 1 && i == len - 1)) // 十进位处于第一位不发壹音RMBStr += HanDigiStr[n];RMBStr += HanDiviStr[i]; // 非零值后加进位,个位为空hasvalue = true; // 置万进位前有值标记} else {if ((i % 8) == 0 || ((i % 8) == 4 && hasvalue)) // 亿万之间必须有非零值方显示万RMBStr += HanDiviStr[i]; // “亿”或“万”}if (i % 8 == 0)hasvalue = false; // 万进位前有值标记逢亿复位lastzero = (n == 0) && (i % 4 != 0);}if (RMBStr.length() == 0)return HanDigiStr[0]; // 输入空字符或"0",返回"零"return RMBStr;}/** * 将浮点型数值转为人民币的表示方式(前面带¥) *  * @param double val(传入的数值) * @return String(转化后的人民币数值表示方式字符串) */public static String NumToRMBStr(double val) {String SignStr = "";String TailStr = "";long fraction, integer;int jiao, fen;if (val < 0) {val = -val;SignStr = "负";}if (val > 99999999999999.999 || val < -99999999999999.999)return "数值位数过大!";// 四舍五入到分long temp = Math.round(val * 100);integer = temp / 100;fraction = temp % 100;jiao = (int) fraction / 10;fen = (int) fraction % 10;if (jiao == 0 && fen == 0) {TailStr = "整";} else {TailStr = HanDigiStr[jiao];if (jiao != 0)TailStr += "角";if (integer == 0 && jiao == 0) // 零元后不写零几分TailStr = "";if (fen != 0)TailStr += HanDigiStr[fen] + "分";}// 下一行可用于非正规金融场合,0.03只显示“叁分”而不是“零元叁分”// if( !integer ) return SignStr+TailStr;return "¥" + SignStr + PositiveIntegerToHanStr(String.valueOf(integer))+ "圆" + TailStr;}/** * 将浮点型数值转为人民币的表示方式 *  * @param double val(传入的数值) * @return String(转化后的人民币数值表示方式字符串) */public static String NumToCNMoney(double val) {String SignStr = "";String TailStr = "";long fraction, integer;int jiao, fen;if (val < 0) {val = -val;SignStr = "负";}if (val > 99999999999999.999 || val < -99999999999999.999)return "数值位数过大!";// 四舍五入到分long temp = Math.round(val * 100);integer = temp / 100;fraction = temp % 100;jiao = (int) fraction / 10;fen = (int) fraction % 10;if (jiao == 0 && fen == 0) {TailStr = "整";} else {TailStr = HanDigiStr[jiao];if (jiao != 0)TailStr += "角";if (integer == 0 && jiao == 0) // 零元后不写零几分TailStr = "";if (fen != 0)TailStr += HanDigiStr[fen] + "分";}// 下一行可用于非正规金融场合,0.03只显示“叁分”而不是“零元叁分”// if( !integer ) return SignStr+TailStr;return SignStr + PositiveIntegerToHanStr(String.valueOf(integer))+ "圆" + TailStr;}public static void main(String[] args) {String amount = MoneyToChinese.NumToCNMoney(456465145);System.out.println(amount);String rmb = MoneyToChinese.NumToRMBStr(8754);System.out.println(rmb);}}

?

热点排行