大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本文实例为大家分享了java阿拉伯数字转换成中文数字的具体代码,供大家参考,具体内容如下
创新互联建站专注于灵寿网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供灵寿营销型网站建设,灵寿网站制作、灵寿网页设计、灵寿网站官网定制、微信小程序服务,打造灵寿网络公司原创品牌,更为您提供灵寿网站排名全网营销落地服务。
package org.lulu.learn.work; import java.io.*; /** * Project: Day07 * Created: Lulu * Date: 2016/8/5 */ public class Work02 { public static void main(String[] args) { // int num = 0; // System.out.println(tranWan(num)); try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("res/data.txt"))); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("res/result.txt"))) ){ String str = ""; int percent = 0; while ((str = br.readLine()) != null) { bw.write(tranWan(Integer.parseInt(str))); bw.newLine(); percent++; Thread.sleep(100); System.out.print("\r["); for (int i = 0; i < 20; i++) { if(i < percent/5){ System.out.print("="); }else if(i == percent/5){ System.out.print(">"); }else{ System.out.print(" "); } } System.out.print("]"); System.out.printf("\t%.2f%%", (float)percent); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } /** * 处理千万数字方法 * * @param num * @return */ private static String tranWan(int num) { StringBuilder builder = new StringBuilder(); if (num / 10000 > 0) {//说明 builder.append(trans(num / 10000)).append("万"); } int temp = num % 10000; if (temp > 0) { String trans = trans(temp); //首先判断是否有万位, if (builder.length() > 0) { //如果千位为0, 则需要补零 if (temp / 1000 == 0) { builder.append("零"); } } builder.append(trans); } if (builder.length() == 0) { builder.append("零"); } return builder.toString(); } /** * 完成4位数转换 * * @param num * @return */ private static String trans(int num) { String[] numeric = new String[]{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"}; StringBuilder builder = new StringBuilder(); builder.append(numeric[num / 1000] + "千"). append(numeric[num / 100 % 10] + "百"). append(numeric[num / 10 % 10] + "十"). append(numeric[num % 10]); //去掉了零千.... int index = -1; while ((index = builder.indexOf(numeric[0], index + 1)) != -1) { if (index < builder.length() - 1) { builder.deleteCharAt(index + 1); } } //去掉双零 index = 0; while ((index = builder.indexOf("零零", index)) != -1) { builder.deleteCharAt(index); } if (builder.length() > 1) { //去掉开头的零 if (builder.indexOf(numeric[0]) == 0) { builder.deleteCharAt(0); } //去掉末尾的零 if (builder.indexOf(numeric[0]) == builder.length() - 1) { builder.deleteCharAt(builder.length() - 1); } } //把开头一十换成十 if (builder.indexOf("一十") == 0) { builder.deleteCharAt(0); } return builder.toString(); } }
再为大家分享一段:java实现阿拉伯数字转换为汉字数字
private static String toChinese(String str) { String[] s1 = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" }; String[] s2 = { "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" }; String result = ""; int n = string.length(); for (int i = 0; i < n; i++) { int num = string.charAt(i) - '0'; if (i != n - 1 && num != 0) { result += s1[num] + s2[n - 2 - i]; } else { result += s1[num]; } System.out.println(" "+result); } System.out.println(result); return result; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("输入字符串:"); String str = scanner.next(); // 将字符串数字转化为汉字 toChinese(str); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。