JAVA人民币大写转换代码
Java代码实现数字金额转换为中文大写形式
Java代码实现数字⾦额转换为中⽂⼤写形式代码:package com.example.test;import java.util.regex.Matcher;import java.util.regex.Pattern;public class MoneyTest {private static final Pattern AMOUNT_PATTERN = pile("^(0|[1-9]\\d{0,11})\\.(\\d\\d)$");private static final char[] RMB_NUMS = {'零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'};private static final String[] UNITS1 = {"元", "⾓", "分", "整"};private static final String[] UNITS2 = {"", "拾", "佰", "仟"};private static final String[] UNITS3 = {"", "万", "亿"};public static void main(String[] args) {System.out.println(numberConvertAmount("100500.32"));}/*** 将⾦额(整数部分等于或少于 12 位,⼩数部分 2 位)转换为中⽂⼤写形式.** @param amount ⾦额数字* @return 中⽂⼤写* @throws IllegalArgumentException*/public static String numberConvertAmount(String amount) throws IllegalArgumentException {// 去掉分隔符amount = amount.replace(",", "");// 验证⾦额正确性if (amount.equals("0.00")) {throw new IllegalArgumentException("⾦额不能为零.");}Matcher matcher = AMOUNT_PATTERN.matcher(amount);if (!matcher.find()) {throw new IllegalArgumentException("输⼊⾦额有误.");}String integer = matcher.group(1); // 整数部分String fraction = matcher.group(2); // ⼩数部分String result = "";if (!integer.equals("0")) {result += integerToRmb(integer) + UNITS1[0]; // 整数部分}if (fraction.equals("00")) {result += UNITS1[3]; // 添加[整]} else if (fraction.startsWith("0") && integer.equals("0")) {result += fractionToRmb(fraction).substring(1); // 去掉分前⾯的[零]} else {result += fractionToRmb(fraction); // ⼩数部分}return result;}// 将⾦额⼩数部分转换为中⽂⼤写private static String fractionToRmb(String fraction) {char jiao = fraction.charAt(0); // ⾓char fen = fraction.charAt(1); // 分return (RMB_NUMS[jiao - '0'] + (jiao > '0' ? UNITS1[1] : ""))+ (fen > '0' ? RMB_NUMS[fen - '0'] + UNITS1[2] : "");}// 将⾦额整数部分转换为中⽂⼤写private static String integerToRmb(String integer) {StringBuilder buffer = new StringBuilder();// 从个位数开始转换int i, j;for (i = integer.length() - 1, j = 0; i >= 0; i--, j++) {char n = integer.charAt(i);if (n == '0') {// 当 n 是 0 且 n 的右边⼀位不是 0 时,插⼊[零]if (i < integer.length() - 1 && integer.charAt(i + 1) != '0') {buffer.append(RMB_NUMS[0]);}// 插⼊[万]或者[亿]if (j % 4 == 0) {if (i > 0 && integer.charAt(i - 1) != '0' || i > 1 && integer.charAt(i - 2) != '0'|| i > 2 && integer.charAt(i - 3) != '0') {buffer.append(UNITS3[j / 4]);}}} else {if (j % 4 == 0) {buffer.append(UNITS3[j / 4]); // 插⼊[万]或者[亿] }buffer.append(UNITS2[j % 4]); // 插⼊[拾]、[佰]或[仟] buffer.append(RMB_NUMS[n - '0']); // 插⼊数字}}return buffer.reverse().toString();}}结果:。
JAVA金额数字转换成大写
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零拾"),
cMoneyStringBuffer.indexOf("零拾") + 2, ZERO);
+ moneyPatternCursor)); moneyPatternCursor = moneyPatternCursor == 8 ? 1 : moneyPatternCursor + 1; } String fractionPart = cMoneyStringBuffer.substring(cMoneyStringBuffer .indexOf(".")); cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("."), cMoneyStringBuffer.length());
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零亿"),
cMoneyStringBuffer.indexOf("零亿") + 2, HUNDRED_MILLION); } while (cMoneyStringBuffer.indexOf("零零") != -1) {
Java人民币金额数字转换成大写
import java.text.NumberFormat; import java.util.HashMap;
public class SimpleMoneyFormat { public static final String EMPTY = ""; public static final String ZERO = "零"; public static final String ONE = "壹"; public static final String TWO = "贰"; public static final String THREE = "叁"; public static final String FOUR = "肆"; public static final String FIVE = "伍"; public static final String SIX = "陆"; public static final String SEVEN = "柒"; public static final String EIGHT = "捌"; public static final String NINE = "玖"; public static final String TEN = "拾"; public static final String HUNDRED = "佰"; public static final String THOUSAND = "仟"; public static final String TEN_THOUSAND = "万"; public static final String HUNDRED_MILLION = "亿"; public static final String YUAN = "元"; public static final String JIAO = "角";
银行小写金额转成大写金额的java方法
Java将数字金额转换成中文大写形式import java.math.BigDecimal;import java.math.RoundingMode;public class ChineseMoneyUtils {/*** 中文数字*/final static private String[] CHINESE_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };/*** 中文数字单位*/final static private String[] CHINESE_NUMBER_UNIT = { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟" };/*** 人民币单位*/final static private String[] CHINESE_MONEY_UNIT = { "圆", "角", "分" };public static void main(String[] args) {String chineseMoney = toChineseMoney(new BigDecimal("320"));System.out.println("chineseMoney = " + chineseMoney);}/*** @param sourceMoney 要转换的数值,最多支持到亿* @return 结果*/public static String toChineseMoney(BigDecimal sourceMoney) {if (new BigDecimal("1000000000000").compareTo(sourceMoney) <= 0&& pareTo(sourceMoney) >= 0) {throw new RuntimeException("支持转换的金额范围为0~1万亿");}StringBuilder sb = new StringBuilder();// 整数部分BigDecimal intPart = sourceMoney.setScale(0, RoundingMode.DOWN);// 小数部分BigDecimal decimalPart = sourceMoney.subtract(intPart).multiply(newBigDecimal(100)).setScale(0,RoundingMode.DOWN);// 处理整数部分圆if (pareTo(BigDecimal.ZERO) > 0) {String intPartNumberString = intPart.toPlainString();int length = intPartNumberString.length();// 统计末尾的零,末尾零不做处理int zeroCount = 0;for (int i = length - 1; i >= 0; i--) {int number = Integer.parseInt(String.valueOf(intPartNumberString.charAt(i)));if (number == 0) {zeroCount++;} else {break;}}for (int i = 0; i < length; i++) {// 如果转换到末尾0,则停止转换if (i + zeroCount == length) {break;}int number = Integer.parseInt(String.valueOf(intPartNumberString.charAt(i)));// 获取中文数字String chineseNumber = CHINESE_NUMBER[number];// 获取中文数字单位String chineseNumberUnit = CHINESE_NUMBER_UNIT[length - i - 1];sb.append(chineseNumber).append(chineseNumberUnit);}// 统计完后加上金额单位sb.append(CHINESE_MONEY_UNIT[0]);} else {sb.append(CHINESE_NUMBER[0]).append(CHINESE_MONEY_UNIT[0]);}// 处理小数部分if (pareTo(new BigDecimal(10)) >= 0) {// 角String jiao = decimalPart.toPlainString();int number = Integer.parseInt(String.valueOf(jiao.charAt(0)));if (number != 0) {String chineseNumber = CHINESE_NUMBER[number];sb.append(chineseNumber).append(CHINESE_MONEY_UNIT[1]);}// 分String fen = decimalPart.toPlainString();number = Integer.parseInt(String.valueOf(fen.charAt(1)));if (number != 0) {String chineseNumber = CHINESE_NUMBER[number];sb.append(chineseNumber).append(CHINESE_MONEY_UNIT[2]);}} else if (pareTo(BigDecimal.ZERO) > 0) {// 分String fen = decimalPart.toPlainString();int number = Integer.parseInt(String.valueOf(fen.charAt(0)));if (number != 0) {String chineseNumber = CHINESE_NUMBER[number];sb.append(chineseNumber).append(CHINESE_MONEY_UNIT[2]);}} else {sb.append("整");}return sb.toString();}}。
java实现人民币数字转大写(转)
java实现⼈民币数字转⼤写(转)希望转换出来的结果为:零元零⾓零分1234希望转换出来的结果为:壹仟贰佰叁拾肆元零⾓零分9999希望转换出来的结果为:玖仟玖佰玖拾玖元零⾓零分1234.5希望转换出来的结果为:壹仟贰佰叁拾肆元伍⾓零分1234.9希望转换出来的结果为:壹仟贰佰叁拾肆元玖⾓零分1234.56希望转换出来的结果为:壹仟贰佰叁拾肆元伍⾓陆分1234.99希望转换出来的结果为:壹仟贰佰叁拾肆元玖⾓玖分1234.994希望转换出来的结果为:壹仟贰佰叁拾肆元玖⾓玖分1234.999希望转换出来的结果为:壹仟贰佰叁拾伍元零⾓零分999999999999.999希望转换出来的结果为:数据过⼤,⽆法进⾏转换,请重新输⼊!package com.tfj.algorithm;import java.util.Scanner;public class RenMingBi {private boolean zero;String[] strHan = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };String[] moneyCount = { "", "拾", "佰", "仟" };RenMingBi() {zero = false;}private String[] divide(double digitalRmbValue) {// 处理整数long intPart = (long) (digitalRmbValue);// 处理⼩数,把⼩数转换成整数形式处理,并进⾏四舍五⼊,此时⼩数四舍五⼊之后为100的应该另外加⼀层处理long decimalPart = Math.round((double) Math.round((digitalRmbValue - intPart) * 10000) / 100);// System.out.println("整数部分为:" + intPart + "⼩数部分为:" + (digitalRmbValue - intPart));if (decimalPart == 100) {intPart += 1;}// System.out.println("四舍五⼊之后整数部分为:" + intPart + "⼩数部分为:" + decimalPart);String strIntPart = "";if (intPart == 0)strIntPart = "0";else {strIntPart = "" + intPart;}String strDecimalPart = "";if (decimalPart < 10 && decimalPart >= 0)strDecimalPart = "0" + decimalPart;else {strDecimalPart = "" + decimalPart;}// 处理输⼊为0的时候if (intPart == 0 && decimalPart == 0) {zero = true;}// System.out.println("准换为String后,整数部分为:" + strIntPart + "⼩数部分为:" + strDecimalPart);return new String[] { strIntPart, strDecimalPart };}// ⽤来转换⼩数部分private String transDecimal(String str) {if (str.equals("100") || str.equals("00")) {return "零⾓零分";} else {String strResult;char[] strRmb = new char[20];strRmb = str.toCharArray();if (strRmb[0] == '0' && strRmb[1] == '0') {strResult = "";} else {int dm = strRmb[0] - '0';int dn = strRmb[1] - '0';strResult = strHan[dm] + "⾓" + strHan[dn] + "分";}return strResult;}}// ⽤来转换整数部分private String transInt(String str) {char[] firstFourBitRmb = new char[20];firstFourBitRmb = str.toCharArray();int len = firstFourBitRmb.length;String strResult = "";for (int i = 0; i < len; i++) {/*** 这⾥开启之后会显⽰成通俗易懂的格式,例如1001,会转化为:壹仟零壹元,⽽不是:壹仟零佰零拾壹元,但我希望得到的是后⾯这种,因为发票所需显⽰的就是后⾯这种 **/// if (firstFourBitRmb[i] == '0' && i < len) {// boolean zeroFlag = false;// while (i < len && firstFourBitRmb[i] == '0') {// zeroFlag = true;// i++;// }// if (i != len && zeroFlag == true)// strResult += "零";// i--;// } else {// int m = firstFourBitRmb[i] - '0';// strResult += strHan[m] + moneyCount[len - i - 1];// }int m = firstFourBitRmb[i] - '0';strResult += strHan[m] + moneyCount[len - i - 1];}return strResult;}public String transComb() {String[] strTempRmb = new String[2];double strValue = 0D;Scanner sc = new Scanner(System.in);System.out.println("请输⼊具体⾦额,结果会精确到分:");strValue = sc.nextDouble();RenMingBi nr = new RenMingBi();strTempRmb = nr.divide(strValue);// 转换⼩数部分String result1 = nr.transDecimal(strTempRmb[1]);// 转换整数部分String result2 = "";int len = strTempRmb[0].length();if (len > 12)return "数据过⼤,⽆法进⾏转换,请重新输⼊!";else {if (len >= 9) {String strBit1 = strTempRmb[0].substring(0, len - 8);String strBit2 = strTempRmb[0].substring(len - 8, len - 4); String strBit3 = strTempRmb[0].substring(len - 4, len);String strResult1 = nr.transInt(strBit1) + "亿";String strResult2 = nr.transInt(strBit2) + "万";String strResult3 = nr.transInt(strBit3) + "元";result2 = strResult1 + strResult2 + strResult3;} else if (len >= 5) {String strBit1 = strTempRmb[0].substring(0, len - 4);String strBit2 = strTempRmb[0].substring(len - 4, len);String strResult1 = nr.transInt(strBit1) + "万";String strResult2 = nr.transInt(strBit2) + "元";result2 = strResult1 + strResult2;} else if (len >= 1) {if (nr.zero == true) {result2 = "零元";} elseresult2 = nr.transInt(strTempRmb[0]) + "元";}return result2 + result1;}}public static void main(String[] args) {RenMingBi rmb = new RenMingBi();while (true) {System.out.println(rmb.transComb());}}}。
java程序:转化金额
java程序:转化⾦额在处理财务账款时,需要将转账⾦额写成⼤写的。
也就是说,如果要转账123456.00元,则需要写成“壹拾贰万叁仟肆佰伍拾陆元整”。
所以常常需要通过程序控制⾃动进⾏转换。
本实例实现了⼩写⾦额到⼤写⾦额的转换。
具体代码如下:import java.text.DecimalFormat;import java.util.*;//@北冥道⼈骑鲲打代码public class f2 {private static String[] numBig = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };private static String[] numInt = { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟" };// 整数单位private static String[] numFloat = { "厘", "分", "⾓" };// ⼩数单位public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("请输⼊⾦额");double money = input.nextDouble();//格式化double数字DecimalFormat df = new DecimalFormat("#0.###");//此时strNum⼩数位最多3位String strNum = df.format(money);if (strNum.indexOf(".") > 0 ) {//判断是否有⼩数String strMoneyInt = strNum.substring(0, strNum.indexOf("."));if(strMoneyInt.length() > 12){System.out.println("数字太⼤了,转换不了");}else{System.out.println(getInt(strNum) + "元" + getDouble(strNum));}}else{if(strNum.length() > 12){System.out.println("数字太⼤了,转换不了");}else{System.out.println(getInt(strNum) + "元整");}}}//整数部分public static String getInt(String str) {if(str.indexOf(".") != -1){str = str.substring(0,str.indexOf("."));//截取⼩数点前⾯的数字}str = new StringBuffer(str).reverse().toString();//反转字符串StringBuffer strB = new StringBuffer(); //创建⼀个空的StringBuffer对象for (int i = 0; i < str.length(); i++){ //把单位添加进去strB.append(numInt[i]);strB.append(numBig[(str.charAt(i)-48)]);//str.charAt(i)-48,这⾥-48是因为str.charAt(i)-48为ASCII码//⽽参照ASCII码://ASCII码为 48 ==》0//ASCII码为 49 ==》1 ...}str = strB.reverse().toString();//把反转过的字符串还原//替换字符串多于的字符if (str.indexOf("零拾") != -1){str = str.replace( "零拾", "零");}if (str.indexOf("零佰") != -1){str = str.replace( "零拾", "零");}if (str.indexOf("零仟") != -1){str = str.replace( "零拾", "零");}if (str.indexOf("零万") != -1){str = str.replace( "零拾", "万");}if (str.indexOf("零亿") != -1){str = str.replace( "零拾", "亿");}if (str.indexOf("零零") != -1){str = str.replace( "零拾", "零");}if (str.indexOf("亿万") != -1){str = str.replace( "零拾", "亿");}//除去零的结尾if (stIndexOf("零") == str.length() - 1) {str = str.substring(0, str.length() - 1);}return str;}//⼩数部分public static String getDouble(String str) {str = str.substring(str.indexOf(".") + 1);//截取⼩数点后的数字//解决单位错位if(str.length() == 1){str = str +"00";}else if(str.length() == 2){str = str +"0";}str = new StringBuffer(str).reverse().toString();//反转字符串StringBuffer strB = new StringBuffer();//创建⼀个空的StringBuffer对象 for (int i = 0; i < str.length(); i++) {//把单位添加进去strB.append(numFloat[i]);strB.append(numBig[str.charAt(i) - 48]);}str = strB.reverse().toString();//把反转过的字符串还原//替换字符串多于的字符if (str.indexOf("零⾓") != -1){str = str.replace( "零⾓", "零");}if (str.indexOf("零分") != -1){str = str.replace( "零分", "零");}if (str.indexOf("零厘") != -1){str = str.replace( "零厘", "零");}if (str.indexOf("零零") != -1){str = str.replace( "零零", "零");}//除去零的结尾if (stIndexOf("零") == str.length() - 1) {str = str.substring(0, str.length() - 1);}return str;}}运⾏截图:。
教你怎么用Java完成人民币大写转化
教你怎么⽤Java完成⼈民币⼤写转化⽬录⼀、前⾔⼆、注意三、思路分析四、完整代码五、代码测试⼀、前⾔⼈民币⼤写⾦额的正确写法:整数部分:零、壹、贰、叁、肆、伍、陆、柒、捌、玖⼩数部分:⾓、分、厘数位部分:拾、佰、仟、万、亿、元⼆、注意阿拉伯数字⼩写⾦额数字中有 “0” 时,中⽂⼤写应按照汉语语⾔规律、⾦额数字构成和防⽌涂改的要求进⾏书写。
举例如下:1、阿拉伯数字中间有 “0” 时,中⽂⼤写要写“零”字,如¥1409.50应写成⼈民币壹仟肆佰零玖元伍⾓。
2、阿拉伯数字中间连续有⼏个 “0” 时、中⽂⼤写⾦额中间可以只写⼀个“零”字,如¥6007.14应写成⼈民币陆仟零柒元壹⾓肆分。
3、阿拉伯⾦额数字万位和元位是 “0” ,或者数字中间连续有⼏个 “0” ,万位、元位也是 “0” 但千位、⾓位不是 “0” 时,中⽂⼤写⾦额中可以只写⼀个零字,也可以不写“零”字,如¥1680.32应写成⼈民币壹仟陆佰捌拾元零叁⾓贰分,或者写成⼈民币壹仟陆佰捌拾元叁⾓贰分。
⼜如¥107000.53应写成⼈民币壹拾万柒仟元零伍⾓叁分,或者写成⼈民币壹拾万零柒仟元伍⾓叁分。
4、阿拉伯⾦额数字⾓位是 “0” ⽽分位不是 “0” 时,中⽂⼤写⾦额“元”后⾯应写“零”字,如¥16409.02应写成⼈民币壹万陆仟肆佰零玖元零贰分,⼜如¥325.04应写成⼈民币叁佰贰拾伍元零肆分。
三、思路分析初始化⼈民币⼤写⾦额,其中数位的顺序是有要求的。
对传⼊的字符串进⾏校验(⾮空、特殊字符)判断字符串的长度是否超过了转换范围判断是否存在负数,对负号进⾏替换分离整数部分和⼩数部分判断整数部分是否已经达到万这⾥使⽤到了⼀个StringUtils类和单元测,junit,需要导⼊依赖<dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies>四、完整代码public class ConvertUpMoney {//整数部分的⼈民币⼤写private static final String[] NUMBERS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};//数位部分private static final String[] IUNIT = {"元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟"};//⼩数部分的⼈民币⼤写private static final String[] DUNIT = {"⾓", "分", "厘"};//转成中⽂的⼤写⾦额public static String toChinese(String str) {//判断输⼊的⾦额字符串是否符合要求if (StringUtils.isBlank(str) || !str.matches("(-)?[\\d]*(.)?[\\d]*")) {System.out.println("抱歉,请输⼊数字!");return str;}//判断输⼊的⾦额字符串if ("0".equals(str) || "0.00".equals(str) || "0.0".equals(str)) {return "零元";}//判断是否存在负号"-"boolean flag = false;if (str.startsWith("-")) {flag = true;str = str.replaceAll("-", "");}//如果输⼊字符串中包含逗号,替换为 "."str = str.replaceAll(",", ".");String integerStr;//整数部分数字String decimalStr;//⼩数部分数字//分离整数部分和⼩数部分if (str.indexOf(".") > 0) {//整数部分和⼩数部分integerStr = str.substring(0, str.indexOf("."));decimalStr = str.substring(str.indexOf(".") + 1);} else if (str.indexOf(".") == 0) {//只存在⼩数部分 .34integerStr = "";decimalStr = str.substring(1);} else { //只存在整数部分 34integerStr = str;decimalStr = "";}//整数部分超出计算能⼒,直接返回if (integerStr.length() > IUNIT.length) {System.out.println(str + ":超出计算能⼒");return str;}//整数部分存⼊数组⽬的是为了可以动态的在字符串数组中取对应的值int[] integers = toIntArray(integerStr);//判断整数部分是否存在输⼊012的情况if (integers.length > 1 && integers[0] == 0) {System.out.println("抱歉,请输⼊数字!");if (flag) {str = "-" + str;}return str;}boolean isWan = isWanUnits(integerStr);//设置万单位//⼩数部分数字存⼊数组int[] decimals = toIntArray(decimalStr);String result = getChineseInteger(integers, isWan) + getChineseDecimal(decimals);//返回最终的⼤写⾦额 if (flag) {return "负" + result;//如果是负数,加上"负"} else {return result;}}//将字符串转为int数组private static int[] toIntArray(String number) {//初始化⼀维数组长度int[] array = new int[number.length()];//循环遍历赋值for (int i = 0; i < number.length(); i++) {array[i] = Integer.parseInt(number.substring(i, i + 1));}return array;}//将整数部分转为⼤写的⾦额public static String getChineseInteger(int[] integers, boolean isWan) {StringBuffer chineseInteger = new StringBuffer("");int length = integers.length;// 对于输⼊的字符串为 "0." 存⼊数组后为 0if (length == 1 && integers[0] == 0) {return "";}for (int i = 0; i < length; i++) {String key = "";//0325464646464if (integers[i] == 0) {if ((length - i) == 13)//万(亿)key = IUNIT[4];else if ((length - i) == 9) {//亿key = IUNIT[8];} else if ((length - i) == 5 && isWan) {//万key = IUNIT[4];} else if ((length - i) == 1) {//元key = IUNIT[0];}if ((length - i) > 1 && integers[i + 1] != 0) {key += NUMBERS[0];}}chineseInteger.append(integers[i] == 0 ? key : (NUMBERS[integers[i]] + IUNIT[length - i - 1]));}return chineseInteger.toString();}//将⼩数部分转为⼤写的⾦额private static String getChineseDecimal(int[] decimals) { //⾓分厘 038 壹分捌厘StringBuffer chineseDecimal = new StringBuffer("");for (int i = 0; i < decimals.length; i++) {if (i == 3) {break;}chineseDecimal.append(decimals[i] == 0 ? "" : (NUMBERS[decimals[i]] + DUNIT[i]));}return chineseDecimal.toString();}//判断当前整数部分是否已经是达到【万】private static boolean isWanUnits(String integerStr) {int length = integerStr.length();if (length > 4) {String subInteger = "";if (length > 8) {subInteger = integerStr.substring(length - 8, length - 4);} else {subInteger = integerStr.substring(0, length - 4);}return Integer.parseInt(subInteger) > 0;} else {return false;}}五、代码测试阿拉伯数字中包含 "0"的测试特殊情况的测试(特殊字符、负数、超过⼈民币数值范围)。
[JAVA]金额小写转大写
private static String[] bits = new String[] { "", "拾", "佰", "仟", "", "拾",
"佰", "仟", "", "拾", "佰", "仟", "", "拾", "佰", "仟", "" };
private static String[] nums = new String[] { "零", "壹", "贰", "叁", "肆", "伍",
"陆", "柒", "捌", "玖" };
public static String getChineseMoney(Double money) {
String[] moneys = moneyStr.split("\\.");
moneyStr = transChineseMoney1(moneys[0])
+ transChineseMoney2(moneys[1]);
return moneyStr;
}
public static String getChineseMoney(String money) {
// System.out.println("打印单个位数值:" + s);
小写转成大写金额 java 代码
}
fraction.insert(0, "元"); // 为整数部分添加标识
} else {
fraction = new StringBuffer("元整");
}
} else {
ms.replace(ms.indexOf("亿万"), ms.indexOf("亿万") + 2, "亿");
}
while (stIndexOf("零") == ms.length() - 1) { // 当结尾为零j,不必显示,经过处理也只可能出现一个零
ms.delete(stIndexOf("零"), stIndexOf("零") + 1);
}
int end;
if ((dotPoint = moneyString.indexOf(".")) != -1) { // 是小数的进入
String fs = moneyString.substring(dotPoint + 1, moneyString
.length());
} else {
moneyStr = moneyString;
}
StringBuffer fraction = null; // 小数部分的处理,以及最后的yuan.
StringBuffer ms = new StringBuffer();
for (int i = 0; i < moneyStr.length(); i++) {
JAVA实现阿拉伯数字金额转汉语大写
JAVA实现阿拉伯数字金额转汉语大写/***将输入金额num转换为汉字大写格式*@param num输入金额(小于10000000)*@return金额的大写格式*/public String translate(double num){String[] upChinese={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖",}; String[] upChinese2={"分","角","圆","拾","佰","仟","萬","拾","佰","仟","亿","拾","佰","仟","兆"};StringBuffer result=new StringBuffer();int count=0;int zeroflag=0;boolean mantissa=false;if(num<0){ //输入值小于零return"输入金额不能为负数!";}if(num==0){ //输入值等于零return"零";}if(String.valueOf(num).indexOf('E')!=-1){ //输入值过大转为科学计数法本方法无法转换return"您输入的金额过大";}int tem=(int)(num*100);if(tem%100==0){ //金额为整时if(tem==0)return"穷鬼别来!"; //输入额为e:0.0012小于分计量单位时 result.insert(0, "整");tem=tem/100;count=2;mantissa=true;}while(tem>0){int t=(int)tem%10; //取得最后一位if(t!=0){ //最后一位不为零时if(zeroflag>=1){ //对该位前的单个或多个零位进行处理if(((!mantissa)&&count==1)){ //不是整数金额且分为为零}else if(count>2&&count-zeroflag<2){ //输入金额为400.04小数点前后都有零result.insert(1,"零");}else if(count>6&&count-zeroflag<6&&count<10){ //万位后为零且万位为零if(count-zeroflag==2){ //输入值如400000 result.insert(0,"萬");}else{result.insert(0,"萬零"); //输入值如400101 }}else if(count>10&&count-zeroflag<10){if(count-zeroflag==2){result.insert(0,"亿");}else{result.insert(0,"亿零");}}else if(((count-zeroflag)==2)){ //个位为零}else if(count>6&&count-zeroflag==6&&count<10){ //以万位开始出现零如4001000result.insert(0,"萬");}else if(count==11&&zeroflag==1){result.insert(0,"亿");}else{result.insert(0,"零");}}result.insert(0,upChinese[t]+ upChinese2[count]);zeroflag=0;}else{if(count==2){result.insert(0,upChinese2[count]); //个位为零补上"圆"字}zeroflag++;}tem/=10;System.out.println("count="+count+"---zero="+zeroflag+"----"+result.toString ());count++;if(count>20)break; }return result.toString(); }}。
JAVA实现数字大写金额转换代码
例子:实现将阿拉伯数字的金额转换成中文大写金额的形式,例如:¥1011 →壹仟零壹拾壹元整。
大写数字为:零、壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿import java.io.*;import ng.String;public class change{public static void main(String agrs[]){//Money用于存放输入的金额数long Money=0;String Mon[]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};String Mon1[]={"拾","佰","仟","万","拾万","百万","仟万","亿","拾亿","百亿","仟亿"},je[]=new String[30];//je[]数组用于存放数字金额被转换后的数字//n用于测试您是否有金额转换int n=agrs.length;if(n==0)System.out.println("请输入需要转换的金额:");//将输入的金额转化成long型的数据else{Money=Long.parseLong(agrs[]); }//bitt用于存放第几个需转换的金额数字,int bitt=0,j,k=-1,i=0;//转换过程while(Money!=0){bitt=(int)(Money%10L);//计算出每个数字对应的大写值for(j=0;j=0;i--)System.out.print(je[i]); }}}}。
java实现数字转换人民币中文大写工具
java实现数字转换⼈民币中⽂⼤写⼯具本⽂实例为⼤家分享了java实现数字转换⼈民币中⽂⼤写的具体代码,供⼤家参考,具体内容如下业务场景:1.在⼈事业务系统开发的报表打印⽂书时经常需要将⼈民币数值转换为⼤写,所以⽤java写了⼀个通⽤的⼤写转换函数。
为了更加⽅便调⽤函数的参数使⽤字符串类型。
例如:825.45 转换后:捌佰贰拾伍元肆⾓伍分2.在⼈事业务系统中经常把⽇期⽤汉字⼤写表⽰,为了⽅便调⽤函数,也是使⽤字符串类型。
例如:2018 转换后:⼆〇⼀⼋下⾯为java代码public class Data2Zh {final static private String NUMBER[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };final static private String NUMBER2[] = { "〇", "⼀", "⼆", "三", "四", "五", "六", "七", "⼋", "九" };final static private String CBit[] = { "", "拾", "佰", "仟" };/*** 将数值⼤写*/public static String capitalization(String szNum) {StringBuilder resstr = new StringBuilder();String tmpstr = szNum.trim();int sl = tmpstr.length();int sp = 0;int dotpos = tmpstr.indexOf('.');if (dotpos != -1) {while (sl > 1 && tmpstr.charAt(sl - 1) == '0')sl--;if (tmpstr.charAt(sl - 1) == '.')sl--;if (sl != tmpstr.length()) {tmpstr = tmpstr.substring(0, sl);}} elsedotpos = sl;if (sl < 1)return NUMBER[0];if (tmpstr.charAt(0) == '-') {resstr.append("负");sp = 1;}String integerNum = tmpstr.substring(sp, dotpos - sp);String decimalNum = "";if (dotpos + 1 < sl)decimalNum = tmpstr.substring(dotpos + 1);sl = integerNum.length();sp = 0;while (sp < sl && integerNum.charAt(sp) == '0')sp++;if (sp > 0)integerNum = integerNum.substring(sp);int inl = integerNum.length();if (inl > 0) {int h = (inl - 1) % 4;int j = (inl - 1) / 4 + 1;sp = 0;boolean allzero = false;boolean preallzero = false;for (; j > 0; j--) {int k = h;h = 3;boolean preiszero = allzero;allzero = true;for (; k >= 0; k--, sp++) {if (integerNum.charAt(sp) == '0')preiszero = true;else {allzero = false;if (preiszero)resstr.append("零");preiszero = false;resstr.append(NUMBER[(byte) (integerNum.charAt(sp)) - 48]).append(CBit[k]);}}// end for kif (/* j!=0 && */ j % 2 == 0) {if (!allzero)resstr.append("万");} else {if (!allzero || !preallzero) {int repyi = j / 2;for (int i = 0; i < repyi; i++)resstr.append("亿");}}preallzero = allzero;}// end for j} elseresstr.append("零");int dnl = decimalNum.length();if (dnl > 0) {resstr.append("点");for (int i = 0; i < dnl; i++) {resstr.append(NUMBER[(byte) (decimalNum.charAt(i)) - 48]);}}return resstr.toString();}/*** 获得某⼀位上的数值,如果 nBit<0 则获得⼩数点后⾯的位数*/static public char getNumByte(String szNum, int nBit) {int sl = szNum.length();int nPos = 0;while (nPos < sl && szNum.charAt(nPos) != '.')nPos++;if (nBit < 0)nPos = nPos - nBit;elsenPos = nPos - nBit - 1;if (nPos < 0 || nPos >= sl)return '0';return szNum.charAt(nPos);}public static String rmbChange(String rmb) {return capitalization((rmb.indexOf('.') >= 0 ? rmb.substring(0, rmb.indexOf('.')) : rmb)) + "元" + capitalization(String.valueOf(getNumByte(rmb, -1))) + "⾓"+ capitalization(String.valueOf(getNumByte(rmb, -2))) + "分";}/*** 仅仅是把 0~9 转换为 "〇","⼀","⼆","三","四","五","六","七","⼋","九"*/public static String changeCN(String szNum) {StringBuilder sb = new StringBuilder();String str = szNum.trim();int sl = str.length();int sp = 0;if (sl < 1)return NUMBER2[0];for (; sp < sl; sp++)if (str.charAt(sp) >= '0' && str.charAt(sp) <= '9')sb.append(NUMBER2[str.charAt(sp) - '0']);elsesb.append(str.charAt(sp));return sb.toString();}public static void main(String[] args) {System.out.println(rmbChange("825.45"));System.out.println(changeCN("2018"));}}效果图:以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
Java实现数字转换为人民币大写
Java实现数字转换为人民币大写四种简单方法//输入一个数字,将它按人民币大写输出package com.bluedot;public class Da2 {public static void main(String[] args) {//改变money的值,不超过9位数。
int money = 100100;int length = 0;char a[] = { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九' };char b[] = { '一', '十', '百', '千', '万', '十', '百', '千', '亿', '十' };int[] c = new int[10];System.out.println(money + "元");while (money > 0) {c[length] = money % 10;// System.out.print(" 第" + length + "位:");// System.out.println(c[length]);money /= 10;length++;}// 位数对照 '一', '十', '百', '千', '万', '十', '百', '千', '亿', '十'// n: 0 1 2 3 4 5 6 7 8 9int n = length - 1;// System.out.println(n);int wanwei = 0; // 记录万位数是否都为零。
java实现将数字转换成人民币大写
java实现将数字转换成⼈民币⼤写Rmb.javapublic class Rmb{/***⼈民币的基本信息和操作*@author weinee*@version 1.0*/double number; //⼈民币的数量private String[] hanArr = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };//汉字计数符号private String[] unitArr = {"", "拾", "佰", "仟"};//汉字计数单位private String[] unitArrs = {"万", "亿", "万", "兆", "万", "亿", "万", "圆"}; //顺序添加单位//private String[] unitsArr = {"万", "亿"}; //汉字计数⼤单位public Rmb(){}/***构造器初始化⼈民币数量*@param 给定的初始化⼈民币数*/public Rmb(double number){this.number = number;}/***把⼀个浮点数分解成long型部分和⼩数部分字符串,把⼈民币换成了整数,浮点数在取⼩数部分时临界值有误差*@return 返回分解后的字符串数组,第⼀个数组元素是整数部分,第⼆个是⼩数部分字符串*/public String[] divideNum(){double num = Math.round(number*100);//对number进⾏四舍五⼊取整long integerPart = (long)num; //连同⼩数点后两位取成整数,避免使⽤减法//double decimalsPart = num-integerPart;//⼩数部分,临界时产⽣了误差String decimalsPartStr;long b = integerPart % 10; //⼩数点后第⼆位long a = (integerPart/10) % 10;//⼩数点后第⼀位integerPart /= 100;if(a==0 && b==0){decimalsPartStr = null;}else{decimalsPartStr = "" + a + b;}return new String[] {String.valueOf(integerPart) , decimalsPartStr};}/***把⼀个数字字符转换成汉语的⼈民币读法*@return 返回汉语⼈民币读法的字符串形式*/public String toHanStr(){String[] results = new String[9]; //⽤来暂时存储每四位分段后的数字字符串String[] resultStrs = new String[9];//⽤来暂时存储每四位分段后转换成的⼈民币读法String result = "";//最终的转换结果String[] divideStr = divideNum(); //得到浮点数分解成的long型部分和⼩数部分字符串,第⼀个数组元素是整数部分字符串,第⼆个是⼩数部分字符串 results[8] = divideStr[1];for (int i=divideStr[0].length(), j=8; i>0&&j>0 ; i-=4,j--){try{results[j-1] = divideStr[0].substring(i-4, i);}catch(Exception e){results[j-1] = divideStr[0].substring(0, i);break;}}if(results[8] == null){resultStrs[8] = "整";}else if(results[8].charAt(1) == '0'){resultStrs[8] = hanArr[results[8].charAt(0) - 48] + "⾓"; //根据ASCII码和hanArr数组吧数字改成汉语⼤写}else{resultStrs[8] = hanArr[results[8].charAt(0) - 48] + "⾓" + hanArr[results[8].charAt(1) - 48] + "分";}for(int i=0; i<8; i++){if(results[i] != null){resultStrs[i] = "";resultStrs[i] += hanArr[results[i].charAt(0) - 48] + unitArr[results[i].length() - 1]; //根据ASCII码和数组长度选择数的单位for (int j=1; j<results[i].length(); j++ )if(results[i].charAt(j-1) == '0' && results[i].charAt(j) != '0')resultStrs[i] += "零" + hanArr[results[i].charAt(j) - 48] + unitArr[results[i].length() - 1 - j]; //根据ASCII码和数组长度选择数的单位 else if(results[i].charAt(j) != '0' )resultStrs[i] += hanArr[results[i].charAt(j) - 48] + unitArr[results[i].length() - 1 - j];}}for (int i=0; i<8; i++ ){if(resultStrs[i] != null){result += resultStrs[i] + unitArrs[i];}}result += resultStrs[8];return result;}}RmbTest.javapublic class RmbTest{public static void main(String[] args){double l;byte[] bye = new byte[50];System.out.println("请输⼊要转换的⼈民币数量:");try{System.in.read(bye);}catch(Exception e){}String s = new String(bye);l = Double.parseDouble(s);Rmb r = new Rmb(l);s = r.toHanStr();System.out.println(s);}}以上所述就是本⽂的全部内容了,希望对⼤家学习java能够有所帮助。
java实现数字转大写的方法
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(convert(135689123)); } }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
这篇文章主要介绍了详解javacallable接口实现多线程的方式文中通过示例代码介绍的非常详细对大家的学习或者工作具有一定的参考学习价值需要的朋友们下面随着小编来一起学习学习吧
java实 现 数 字 转 大 写 的 方 法
java实现数字转大写的方法
说明:ห้องสมุดไป่ตู้
将数字金额转大写,如下:
public class Test { /** * @param args * add by zxx ,Nov 29, 2008 */ private static final char[] data = new char[] { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' };
private static final char[] units = new char[] { '元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿' };
Java实现人民币大写金额转数字
Java实现⼈民币⼤写⾦额转数字1、单位和⾦额映射。
单位数组添加的顺序需要按从⼤到⼩排序;⽀持错别字,只需要添加到列表元素Entry的值数组中;拾即是单位也是⾦额(⽐如壹拾元整,此时拾是单位;拾元整,此时拾是⾦额) /** 计量单位映射,按从⼤到⼩的顺序。
**/private static List<Entry<String, String[]>> units = new ArrayList<>();/** ⾦额数字映射。
**/private static List<Entry<String, String[]>> numbers = new ArrayList<>();static {units.add(new SimpleEntry<>("100000000", new String[] { "亿" }));units.add(new SimpleEntry<>("10000", new String[] { "万" }));units.add(new SimpleEntry<>("1000", new String[] { "仟" }));units.add(new SimpleEntry<>("100", new String[] { "佰" }));units.add(new SimpleEntry<>("10", new String[] { "拾" }));units.add(new SimpleEntry<>("1", new String[] { "圆" }));units.add(new SimpleEntry<>("0.1", new String[] { "⾓" }));units.add(new SimpleEntry<>("0.01", new String[] { "分" }));numbers.add(new SimpleEntry<>("0", new String[] { "零" }));numbers.add(new SimpleEntry<>("1", new String[] { "壹" }));numbers.add(new SimpleEntry<>("2", new String[] { "贰" }));numbers.add(new SimpleEntry<>("3", new String[] { "叁" }));numbers.add(new SimpleEntry<>("4", new String[] { "肆" }));numbers.add(new SimpleEntry<>("5", new String[] { "伍" }));numbers.add(new SimpleEntry<>("6", new String[] { "陆" }));numbers.add(new SimpleEntry<>("7", new String[] { "柒" }));numbers.add(new SimpleEntry<>("8", new String[] { "捌" }));numbers.add(new SimpleEntry<>("9", new String[] { "玖" }));numbers.add(new SimpleEntry<>("10", new String[] { "拾" }));}2、⼤写转数字。
用java代码将金额转换成中文大写
import java.util.regex.Matcher;import java.util.regex.Pattern;public class getChinestMoney {public static void main(String[] args) {System.out.println("原金额为:154846244.20");System.out.println("转换后的金额为:");System.out.println(ChineseMoney("154846244.20"));}/*** 获得中文大写的金额的整数和小数部分* @param money* @return*/public static String ChineseMoney(String money) {String text = transChineseMoney1(money) + transChineseMoney2(money);//"Pattern.CASE_INSENSITIVE"表示不对大小写进行区分Pattern p = pile("零分",Pattern.CASE_INSENSITIVE);Matcher m = p.matcher(text);text = m.replaceAll("");return text;}/*** 截取金额的整数部分,并将其转换成中文大写格式* @return*/public static String transChineseMoney1(String s){ String ss = s;String tmpnewchar = "";//以“.”将ss分成两段,part[0]为整数部分,part[1]为小数部分 String[] part = ss.split("\\.");if (part[0].length() > 10) {// 超出可转换位数return"";}for (int i = 0; i < part[0].length(); i++) {char perchar = part[0].charAt(i);if (perchar == '0')tmpnewchar = tmpnewchar + "零";if (perchar == '1')tmpnewchar = tmpnewchar + "壹";if (perchar == '2')tmpnewchar = tmpnewchar + "贰";if (perchar == '3')tmpnewchar = tmpnewchar + "叁";if (perchar == '4')tmpnewchar = tmpnewchar + "肆";if (perchar == '5')tmpnewchar = tmpnewchar + "伍";if (perchar == '6')tmpnewchar = tmpnewchar + "陆";if (perchar == '7')tmpnewchar = tmpnewchar + "柒";if (perchar == '8')tmpnewchar = tmpnewchar + "捌";if (perchar == '9')tmpnewchar = tmpnewchar + "玖";int j = part[0].length() - i - 1;if (j == 0)tmpnewchar = tmpnewchar + "圆";if (j == 1 && perchar != 0)tmpnewchar = tmpnewchar + "拾";if (j == 2 && perchar != 0)tmpnewchar = tmpnewchar + "佰";if (j == 3 && perchar != 0)tmpnewchar = tmpnewchar + "仟";if (j == 4 && perchar != 0)tmpnewchar = tmpnewchar + "万";if (j == 5 && perchar != 0)tmpnewchar = tmpnewchar + "拾";if (j == 6 && perchar != 0)tmpnewchar = tmpnewchar + "佰";if (j == 7 && perchar != 0)tmpnewchar = tmpnewchar + "仟";if (j == 8 && perchar != 0)tmpnewchar = tmpnewchar + "亿";if (j == 9 && perchar != 0)tmpnewchar = tmpnewchar + "拾";}return tmpnewchar;}/*** 截取金额的小数部分,并将其转换成中文大写格式* @return*/public static String transChineseMoney2(String s) {String ss = s;String tmpnewchar1 = "";String[] part = ss.split("\\.");if (ss.indexOf(".") != -1) {if (part[1].length() > 2) {// MessageDialog.openInformation(null,"提示","小数点之后只能保留两位,系统将自动截段");part[1] = part[1].substring(0, 2);}for (int i = 0; i < part[1].length(); i++) {char perchar = part[1].charAt(i);// System.out.println(perchar);if (perchar == '0')tmpnewchar1 = tmpnewchar1 + "零";if (perchar == '1')tmpnewchar1 = tmpnewchar1 + "壹";if (perchar == '2')tmpnewchar1 = tmpnewchar1 + "贰";if (perchar == '3')tmpnewchar1 = tmpnewchar1 + "叁";if (perchar == '4')tmpnewchar1 = tmpnewchar1 + "肆";if (perchar == '5')tmpnewchar1 = tmpnewchar1 + "伍";if (perchar == '6')tmpnewchar1 = tmpnewchar1 + "陆";if (perchar == '7')tmpnewchar1 = tmpnewchar1 + "柒";if (perchar == '8')tmpnewchar1 = tmpnewchar1 + "捌";if (perchar == '9')tmpnewchar1 = tmpnewchar1 + "玖";if (i == 0 && perchar != 0)tmpnewchar1 = tmpnewchar1 + "角";if (i == 1 && perchar != 0)tmpnewchar1 = tmpnewchar1 + "分"; }}return tmpnewchar1;}}。
java实现数字金额转换成汉字大写金额
java实现数字⾦额转换成汉字⼤写⾦额我们经常在做项⽬的财务报表,需要打印发票等情况时需要⽤到⼩写的财务⾦额要转换成汉字的⼤写⾦额,下⾯就是实现该功能的代码import ng.Math;/***通⽤型⾦钱转换类**/public class Money {private String HanDigiStr[] = new String[] { "零", "壹", "贰", "叁", "肆", "伍","陆", "柒", "捌", "玖" };private String HanDiviStr[] = new String[] { "", "拾", "佰", "仟", "万", "拾","佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰","仟", "万", "拾", "佰", "仟" };/*** @param NumStr 输⼊字符串必须正整数,只允许前导空格(必须右对齐),不宜有前导零* @return*/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 && (lastzero || i==len-1) ) ) //// 如⼗进位前有零也不发壹⾳⽤此⾏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;}。
Java人民币小写转大写字符串的实现
Java⼈民币⼩写转⼤写字符串的实现写了⼀个⼈民币⼩写转⼤写的⽅法,Java版本,思路很简单,没有测出什么Bug,有bug欢迎反馈public class RMBChange {private static String[] nums = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};// 单位还可以往上private static String[] unit = {"元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万"};// ⼩数位如还有需求可以直接往这个数组⾥加,private static String[] countNum = {"⾓", "分"};public static String change(String money) {// 过滤空if (money == null || "".equals(money)) {return "";}StringBuilder res = new StringBuilder();String[] splitStr = money.split("\\.");if (splitStr.length > 2) {throw new RuntimeException("输⼊的参数不是数字!");}String front = splitStr[0];// ⽤于判定0的显⽰boolean isZero = true;if (front.length() > unit.length) {throw new RuntimeException("输⼊的参数⼤于万亿!");}for (int i = 0; i < front.length(); i++) {// 整数位处理int dw = front.length() - i - 1;// ⽤ASCII码获得数字int index = (front.charAt(i) - '0');if (index == 0) {isZero = true;if (dw == 0 || dw == 4 || dw == 8) {// 元,万,亿需要拼接res.append(unit[dw]);}} else {if (isZero && i != 0) {// 多个0只显⽰⼀个res.append(nums[0]);}isZero = false;res.append(nums[index]);res.append(unit[dw]);}}// 判断是否有⼩数位if (splitStr.length > 1) {// ⼩数位处理,如果不需要则直接删除这个if及内部所有内容isZero = true;String back = splitStr[1];if (back.length() > countNum.length) {throw new RuntimeException("⼩数位⼩于【" + countNum[countNum.length - 1] + "】!");}for (int i = 0; i < back.length(); i++) {int index = (back.charAt(i) - '0');if (index == 0) {isZero = true;} else {if (isZero && i != 0) {res.append(nums[0]);}isZero = false;res.append(nums[index]);res.append(countNum[i]);}}}return res.toString();}}测试数据public static void main(String[] args) {System.out.println(change("9999999999999.99")); // 玖万玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰玖拾玖元玖⾓玖分System.out.println(change("5020055606500.00")); // 伍万零贰佰亿零伍仟伍佰陆拾万零陆仟伍佰元System.out.println(change("4001500050.16")); // 肆拾亿零壹佰伍拾万零伍拾元壹⾓陆分System.out.println(change("7854416")); // 柒佰捌拾伍万肆仟肆佰壹拾陆元System.out.println(change("9556")); // 玖仟伍佰伍拾陆元System.out.println(change("10086.0")); // 壹万零捌拾陆元System.out.println(change("263341.26")); // 贰拾陆万叁仟叁佰肆拾壹元贰⾓陆分System.out.println(change("3651.06")); // 叁仟陆佰伍拾壹元零陆分System.out.println(change("1.00")); // 壹元System.out.println(change("300.00")); // 叁佰元System.out.println(change("2007.01")); // 贰仟零柒元零壹分}输出结果玖万玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰玖拾玖元玖⾓玖分伍万零贰佰亿零伍仟伍佰陆拾万零陆仟伍佰元肆拾亿零壹佰伍拾万零伍拾元壹⾓陆分柒佰捌拾伍万肆仟肆佰壹拾陆元玖仟伍佰伍拾陆元壹万零捌拾陆元贰拾陆万叁仟叁佰肆拾壹元贰⾓陆分叁仟陆佰伍拾壹元零陆分壹元叁佰元贰仟零柒元零壹分到此这篇关于Java⼈民币⼩写转⼤写字符串的实现的⽂章就介绍到这了,更多相关Java⼈民币⼩写转⼤写字符串内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!。
java 人民币大写转换
publicclass RMB2UpCase {publicstatic String changeToBig(double value) {char[] hunit = { '拾', '佰', '仟' };char[] vunit = { '万', '亿' };char[] digit = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' };long midVal = (long) (value * 100); // 转化成整形String valStr = String.valueOf(midVal); // 转化成字符串String head = valStr.substring(0, valStr.length() - 2); // 取整数部分String rail =valStr.substring(valStr.length() - 2); // 取小数部分String prefix = ""; // 整数部分转化的结果String suffix = ""; // 小数部分转化的结果// 处理小数点后面的数if (rail.equals("00")) { // 如果小数部分为0suffix = "整";} else {//rail.charAt(0) - '0'进行运算过后就是数字 suffix = digit[rail.charAt(0) - '0'] + "角"+ digit[rail.charAt(1) - '0'] + "分"; // 否则把角分转化出来}// 处理小数点前面的数char[] chDig = head.toCharArray(); // 把整数部分转化成字符数组char zero = '0'; // 标志'0′表示出现过0byte zeroSerNum = 0; // 连续出现0的次数for (int i = 0; i<chDig.length; i++) { int idx = (chDig.length - i - 1) % 4; // 取段内位置int vidx = (chDig.length - i - 1) / 4; // 取段位置if (chDig[i] == '0') { // 如果当前字符是0 zeroSerNum++; // 连续0次数递增if (zero == '0') { // 标志第一次出现0zero = digit[0];//idex==0表示是段位的结尾;zeroSerNum表示这个段位有数字,不全部为0} elseif (idx == 0 &&vidx> 0&&zeroSerNum< 4) {prefix += vunit[vidx - 1];//单位:万,亿zero = '0';}continue;}zeroSerNum = 0; // 连续0次数清零if(zero != '0') { // 如果标志不为0,则加上,例如万,亿什么的prefix += zero;zero = '0';}prefix += digit[chDig[i] - '0']; // 转化该数字表示if (idx> 0)prefix += hunit[idx - 1];//单位:拾,佰,仟//段位大于0,段内位置是末尾if (idx == 0 &&vidx> 0) {prefix += vunit[vidx - 1]; // 段结束位置应该加上段名如万,亿}}if (prefix.length() > 0)prefix += '圆'; // 如果整数部分存在,则有圆的字样return prefix + suffix; // 返回正确表示 }publicstaticvoid main(String[] args) { System.out.println(changeToBig(9299999 99.01));}}。
人民币小写金额转大写金额(java版)
人民币小写金额转大写金额(java版)代码说明:1.最大支持百万亿(15位整数,2位小数) ;或仟万亿(16位整数)2. 超过最大支持仟万亿(16位整数),只输出大写数字3. 整数部分越大,则小数部分输出越不精确4. 负数,则忽略负号代码:1.原代码import java.text.DecimalFormat;/**小写转大写* @author lily200825 2011-5-16* <li>最大支持<font color='red'>仟万亿</font>(16位整数,2位小数)</li>*/public class RMB {//输出日志public static volatile boolean isDebug = false;//阶值{仟万亿。
仟,佰,拾,元}public static final String[] unit = { "仟","佰","拾","万","仟","佰","拾","亿","仟","佰","拾","万","仟" ,"佰","拾","元" };// 数字表示public static final char[] digit = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌','玖' };/*** 小写金额转大写金额* <li>最大支持<font color='red'>百万亿</font>(15位整数,2位小数) ;或<font color='red'>仟万亿</font>(16位整数)</li>* <li>超过最大支持<font color='red'>仟万亿</font>(16位整数),只输出大写数字</li> * <li>整数部分越大,则小数部分输出越不精确</li>* <li>负数,则忽略负号</li>* @param amount* @return 大写金额*/public static String number2UpperCase(double _amount){double amount = Math.abs(_amount);StringBuffer result = new StringBuffer();/** -------- 整数部分--------*/StringBuffer result_intStr = new StringBuffer();long num = (long)amount;// 转成大写String intStr = getUpperCase(num);int len = intStr.length();String regex_01 = "(零.)+";String regex_00 = "(零)+";String rep_str = "零";char zero = '零';if(len<=16){// ====== 仟万亿======int index = 0;for(int i=16-len ; i<16; i++){result_intStr.append(intStr.charAt(index++));//加权值result_intStr.append(unit[i]);}//加权后,未合并连续零String temp_intStr = result_intStr.toString();int temp_len = temp_intStr.length();if(isDebug){System.out.println("加权后未处理连续零,["+temp_intStr+"]。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
int lie = 0;
int len = chDig.length;
if (len % 4 != 0) {
hang = len / 4;
lie = len % 4 - 2;
} else {
hang = len / 4 - 1;
lie = 2;
}
for (int i = 0; i < len - 1; i++) {// 整数部分除了最后一位的所有数
if (chDig[i] == '0' && chDig[i + 1] == '0') {
preZero = false;
} else if (chDig[i] == '0' && chDig[i + 1] != '0') {
}
if (end.equals("00")) {
suffix = "圆整";
} else {
if (end.charAt(0) != '0') {
suffix += digit[end.charAt(0) - '0'] + "角";
}
if (end.charAt(1) != '0') {
char[] vunit = { '万', '亿' };
char[] digit = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' };
Scanner sca = new Scanner(System.in);
System.out.println("请输入要转换为大写的数字:");
}
}
if (chDig[len - 1] != '0') {// 整数部分最后一位数
prefix += String.valueOf(digit[chDig[len - 1] - '0']);
}
System.out.println(prefix + suffix);
}
}
double num = sca.nextDouble();//输入一个值
long num_long = (long) (num * 100);
String num_String = String.valueOf(num_long);
String head = "0";// 整数部分
if (num_String.length() > 2) {
suffix = "圆";
head = num_String.substring(0, num_String.length() - 2);
end = num_String.substring(num_String.length() - 2);
}
}
if (hang > 0 && lie == -1) {
prefix += String.valueOf(vunit[hang - 1]);
hang--;
}
lie--;
if (lie == -2) {
lie = 2;
String end = String.valueOf(num_long);// 小数部分
if (num_long < 10) {
end = "0" + String.valueOf(num_long);
}
String suffix = "";// 小数部分转换的结果
/**
* 功能:实现数字大写转换
*/
package Demo;
import java.util.Scanner;
public class Demo1 {
public static voiቤተ መጻሕፍቲ ባይዱ main(String[] args) {
char[] hunit = { '拾', '佰', '仟' };
suffix += digit[end.charAt(1) - '0'] + "分";
}
}
String prefix = "";// 整数部分转换的结果
char[] chDig = head.toCharArray();
boolean preZero = true;
preZero = true;
}
if (preZero) {
prefix += String.valueOf(digit[chDig[i] - '0']);
if (lie >= 0 && chDig[i] != '0') {
prefix += String.valueOf(hunit[lie]);