Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of RMB uppercase conversion by Java

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "Java how to achieve RMB uppercase conversion", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Java how to achieve RMB uppercase conversion" article.

I. Preface

The correct way to write the capital amount of RMB:

Integer parts: zero, one, two, three, restaurant, Wu, Lu, Qi, Qi, Jiu

Decimal part: angle, minute, percentage point

Digital part: pick up, hundreds, thousands, tens of millions, billions, yuan

Pay attention to

When there is a "0" in the Arabic numeral lowercase amount, the Chinese capitalization shall be written in accordance with the rules of Chinese language, the composition of the amount number and the requirements to prevent alteration. Examples are as follows:

1. When there is a "0" in the middle of Arabic numerals, the word "zero" should be written in Chinese capitals. for example, RMB 1409.50 should be written as RMB 1000 yuan.

2. When there are several consecutive "zeros" in Arabic numerals, only one "zero" can be written in the middle of the Chinese capital amount. For example, ¥6007.14 should be written as RMB Lu Qianqi Yuan 10.

3. The ten thousand digits and digits of the Arabic amount are "0", or there are several consecutive "zeros" in the middle of the numbers, and the ten thousand digits and digits are also "0", but when the thousand digits and corner digits are not "0", you can write only one zero character in the Chinese capital amount. Or you can not write the word "zero".

For example, 1680.32 should be written as RMB 1, 000, lxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx For example, ¥107000.53 should be written as one hundred thousand thousand yuan and three cents, or one hundred thousand thousand dollars and three cents.

4. When the numeral corner of the Arabic amount is "0" and the quantile is not "0", the word "zero" should be written after the Chinese capital amount "yuan". For example, RMB 16409.02 should be written as RMB 10,000,000 yuan and RMB 325.04 should be written as RMB 300,000 yuan.

Third, train of thought analysis

Initialize the uppercase amount of RMB, where the order of digits is required.

Check the incoming string (non-empty, special characters)

Determine whether the length of the string exceeds the conversion range

Determine whether there is a negative number and replace the negative sign

Separate the integer part from the fractional part

Judge whether the integer part has reached ten thousand.

Here we use a StringUtils class and unit test, junit, which needs to import dependencies

Commons-lang commons-lang 2.6 junit junit 4.12 test IV, complete code public class ConvertUpMoney {/ / integer part of the RMB capital private static final String [] NUMBERS = {"zero", "one", "two", "three", "restaurant", "Wu", "Lu" "Qi", "Jiu", "Jiu"} / / Digital part private static final String [] IUNIT = {"yuan", "pick", "hundred", "thousand", "ten thousand", "pick up", "hundred", "thousand", "billion", "pick up", "hundred", "thousand", "ten thousand", "pick up", "hundred", "thousand"} / / decimal RMB capital private static final String [] DUNIT = {"jiao", "fen", "PCT"} / / convert uppercase amount public static String toChinese (String str) {/ / to determine whether the entered amount string meets the requirements if (StringUtils.isBlank (str) | |! str.matches ("(-)? [\ d] * (.)? [\ d] *") {System.out.println ("Sorry, please enter a number!") ; return str;} / / determines the entered amount string if ("0" .equals (str) | | "0.00" .equals (str) | | "0.0" .equals (str)) {return "zero yuan";} / / determines whether there is a negative sign "-" boolean flag = false " If (str.startsWith ("-")) {flag = true; str = str.replaceAll ("-", ");} / / if the input string contains a comma, replace it with". " Str = str.replaceAll (",", "."); String integerStr;// integer part number String decimalStr;// decimal part number / separate integer part and decimal part if (str.indexOf (".") > 0) {/ / integer part and decimal part integerStr = str.substring (0, str.indexOf (".")) DecimalStr = str.substring (str.indexOf (".") + 1);} else if (str.indexOf (".") = 0) {/ / only the fractional portion. 34 integerStr = "; decimalStr = str.substring (1);} else {/ / only the integer portion 34 integerStr = str; decimalStr =" } / / if the integer exceeds the computing power, return if (integerStr.length () > IUNIT.length) {System.out.println (str + ": beyond the computing power"); return str } / / the integer part is stored in the array so that the corresponding value int [] integers = toIntArray (integerStr) can be dynamically taken in the string array; / / to determine whether there is an input 012 in the integer part if (integers.length > 1 & & integers [0] = 0) {System.out.println ("Sorry, please enter a number!") ; if (flag) {str = "-" + str;} return str;} boolean isWan = isWanUnits (integerStr); / / set ten thousand units / / decimal digits to be stored in the array int [] decimals = toIntArray (decimalStr); String result = getChineseInteger (integers, isWan) + getChineseDecimal (decimals) / / return the final uppercase amount if (flag) {return "negative" + result;// if it is negative, add "negative"} else {return result;}} / / convert the string to int array private static int [] toIntArray (String number) {/ / initialize the one-dimensional array length int [] array = new int [number.length ()] / / Loop through the assignment 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." 存入数组后为 0 if (length == 1 && integers[0] == 0) { return ""; } for (int i = 0; i < length; i++) { String key = "";//0325464646464 if (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 ();} / the amount converted to uppercase by fractional part 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 5. Code testing

A test containing "0" in Arabic numerals

Testing of special cases (special characters, negative numbers, beyond the range of RMB values).

@ Test public void test () {String number = "1409.50"; String afterStr = ConvertUpMoney.toChinese (number); System.out.println (number + ":" + afterStr); / / String number = "6007.14"; String afterStr = ConvertUpMoney.toChinese (number); System.out.println (number + ":" + afterStr) / / Lu Qianqian Yuan 10 cents} @ Test public void test3 () {String number = "1680.32"; String afterStr = ConvertUpMoney.toChinese (number); System.out.println (number + ":" + afterStr); / / Lu Qianbai LXXXVI Yuan 10 cents} @ Test public void test4 () {String number = "107000.53"; String afterStr = ConvertUpMoney.toChinese (number) System.out.println (number + ":" + afterStr); / / thousands of thousands of dollars) @ Test public void test5 () {String number = "16409.02"; String afterStr = ConvertUpMoney.toChinese (number); System.out.println (number + ":" + afterStr) / 10 thousand nine yuan zero two cents} @ Test public void test6 () {String number = "325.04"; String afterStr = ConvertUpMoney.toChinese (number); System.out.println (number + ": / / Sorry, please enter a number! System.out.println (number + ":" + afterStr);} @ Test public void test8 () {String number = "325#sdr"; String afterStr = ConvertUpMoney.toChinese (number); / / Sorry, please enter a number! System.out.println (number + ":" + afterStr); / / 325#sdr: 325#sdr} @ Test public void test9 () {String number = "- 34327987"; String afterStr = ConvertUpMoney.toChinese (number); / /-34327987: minus 3, 000, 000 } @ Test public void test10 () {String number = "78934029675923532"; System.out.println ("length:" + number.length ()); / / length: 17 String afterStr = ConvertUpMoney.toChinese (number); / / 78934029675923532: out of computing capacity System.out.println (number + ":" + afterStr) } the above is the content of this article on "how to achieve RMB uppercase conversion in Java". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report