时间:2023-12-07来源:系统城装机大师作者:佚名
在日常生活中,我们经常会将阿拉伯数字转化为中文大写的情况:"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾", "佰", "仟", "万", "亿",因此自己就编写一个类,实现该功能的转化。
实现过程:
对于这个问题的实现,我们首先把数字按四位一个拆开(符合我们读数字的习惯),然后对这四位数,做出他的读法,具体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
private static String getNumberStr1000 ( int num) { if (num > 9999 || num < 0 ) { return "" ; } int q = num / 1000 ; int b = (num / 100 ) % 10 ; int s = (num / 10 ) % 10 ; int g = num % 10 ; StringBuffer sb = new StringBuffer(); //千 if (q > 0 ) { sb.append(NUMBER1[q]); sb.append(NUMBER2[ 3 ]); } //百 if (b > 0 ) { sb.append(NUMBER1[b]); sb.append(NUMBER2[ 2 ]); } else { if (q != 0 ) { sb.append(NUMBER2[ 0 ]); } } //十 if (s > 0 ) { sb.append(NUMBER1[s]); sb.append(NUMBER2[ 1 ]); } else { if (b != 0 ) { sb.append(NUMBER2[ 0 ]); } } //个 if (g > 0 ) { sb.append(NUMBER1[g]); } return sb.toString(); } |
下面我们只需要在编写一个方法,实现每个四位数的读法以及对应的单位即可,具体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
public static String getNumberStr( int num) { if (num < 0 ) { return "" ; } if (num == 0 ) { return NUMBER1[ 0 ]; } int split = 10000 ; int y = num / (split * split); int w = (num / split) % split; int g = num % split; StringBuffer sb = new StringBuffer(); //亿 if (y > 0 ) { sb.append(getNumberStr1000(y)); sb.append(NUMBER2[ 5 ]); } //万 if (w > 999 ) { sb.append(getNumberStr1000(w)); sb.append(NUMBER2[ 4 ]); } else { if (w > 0 ) { if (y != 0 ) { sb.append(NUMBER2[ 0 ]); } sb.append(getNumberStr1000(w)); sb.append(NUMBER2[ 4 ]); } } //万以下 if (g > 0 ) { if (w != 0 ) { if (g > 999 ) { sb.append(getNumberStr1000(g)); } else { sb.append(NUMBER2[ 0 ]); sb.append(getNumberStr1000(g)); } } else { if (y != 0 ) { sb.append(NUMBER2[ 0 ]); } sb.append(getNumberStr1000(g)); } } return sb.toString(); } |
完整源代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
/** *@Description: */ package com.lulei.util; public class NumberUtil { private static String[] NUMBER1 = { "零" , "壹" , "贰" , "叁" , "肆" , "伍" , "陆" , "柒" , "捌" , "玖" }; private static String[] NUMBER2 = { "零" , "拾" , "佰" , "仟" , "万" , "亿" }; /** * @param num * @return * @Author:lulei * @Description:将数字转化为大写 */ public static String getNumberStr( int num) { if (num < 0 ) { return "" ; } if (num == 0 ) { return NUMBER1[ 0 ]; } int split = 10000 ; int y = num / (split * split); int w = (num / split) % split; int g = num % split; StringBuffer sb = new StringBuffer(); //亿 if (y > 0 ) { sb.append(getNumberStr1000(y)); sb.append(NUMBER2[ 5 ]); } //万 if (w > 999 ) { sb.append(getNumberStr1000(w)); sb.append(NUMBER2[ 4 ]); } else { if (w > 0 ) { if (y != 0 ) { sb.append(NUMBER2[ 0 ]); } sb.append(getNumberStr1000(w)); sb.append(NUMBER2[ 4 ]); } } //万以下 if (g > 0 ) { if (w != 0 ) { if (g > 999 ) { sb.append(getNumberStr1000(g)); } else { sb.append(NUMBER2[ 0 ]); sb.append(getNumberStr1000(g)); } } else { if (y != 0 ) { sb.append(NUMBER2[ 0 ]); } sb.append(getNumberStr1000(g)); } } return sb.toString(); } /** * @param num * @return * @Description:对万以下的数字进行大小写转化 */ private static String getNumberStr1000 ( int num) { if (num > 9999 || num < 0 ) { return "" ; } int q = num / 1000 ; int b = (num / 100 ) % 10 ; int s = (num / 10 ) % 10 ; int g = num % 10 ; StringBuffer sb = new StringBuffer(); //千 if (q > 0 ) { sb.append(NUMBER1[q]); sb.append(NUMBER2[ 3 ]); } //百 if (b > 0 ) { sb.append(NUMBER1[b]); sb.append(NUMBER2[ 2 ]); } else { if (q != 0 ) { sb.append(NUMBER2[ 0 ]); } } //十 if (s > 0 ) { sb.append(NUMBER1[s]); sb.append(NUMBER2[ 1 ]); } else { if (b != 0 ) { sb.append(NUMBER2[ 0 ]); } } //个 if (g > 0 ) { sb.append(NUMBER1[g]); } return sb.toString(); } public static void main(String[] args) { // TODO Auto-generated method stub int i = 1 ; System.out.println(i + "--" + NumberUtil.getNumberStr(i)); i = 1001 ; System.out.println(i + "--" + NumberUtil.getNumberStr(i)); i = 100101 ; System.out.println(i + "--" + NumberUtil.getNumberStr(i)); i = 10100101 ; System.out.println(i + "--" + NumberUtil.getNumberStr(i)); i = 1234567890 ; System.out.println(i + "--" + NumberUtil.getNumberStr(i)); } } |
测试运行结果:
希望本文所述对大家学习java程序设计有所帮助。
2023-12-07
使用Navicat Premium将SQLServer数据导出为sql格式2023-12-07
SQLServer 2008 R2中使用Cross apply统计最新数据和最近数据2023-12-07
SQL Server 2008 评估期已过怎么解决今天以前一直用的SQL Server 2005做开发,偶尔也用MySQL,现入手公司项目,用到SQL Server 2008,于是乎必须安装它,免得出现其他很纠结的小问题,现将自己安装图解分享如下:1、点开其S...
2023-12-07
在安装SQL 2008的时候,遇到了一个小问题,小编这里为大家解决一下。工具:SQLFULL_CHS.iso方法/步骤:点开电脑左下角的开始按钮,在弹出的菜单中选中运行输入框。在运行的输入框输入...
2023-12-07