java阿拉伯数字转中文数字
程序员文章站
2023-12-12 15:27:28
本文实例为大家分享了java阿拉伯数字转换成中文数字的具体代码,供大家参考,具体内容如下
package org.lulu.learn.work;
impo...
本文实例为大家分享了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); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。