Java Amount Conversion _ Arabic numerals into Chinese traditional form

Source: Internet
Author: User

I saw this topic on the internet there are a lot of ways, I have come up with a way, but because of the time is not enough, can only convert the whole number, did not get into the corner.

Import Java.io.bufferedreader;import java.io.ioexception;import java.io.inputstreamreader;/* * Idea: * 1. Define two tables, Stores Chinese numbers and currency units. * 2. Keyboard input using IO stream BufferedReader to receive keyboard input. * 3. The input Arabic numerals into a character array, the position of each digit in the corner of the table to convert the Chinese number, * after the conversion and then from the corresponding position to insert the currency unit. * 4. Modify the format to match the print output. */public class Moneyconvert {public static void main (string[] args) {//Create a table with a Chinese capital number and a unit of currency in tables string[] cnnum = {"0", "one", "Ii.", " Three "," the "," WU "," Lu "," Qi "," ba "," JIU "}; string[] unit = {"", "Pick", "Bai", "Thousand", "Million", "Pick", "Bai", "Qian", "billion", "Pick Up", "Bai", "Thousand"}; System.out.println ("Please enter Amount:");//Create stream object for receiving keyboard input data bufferedreader in = new BufferedReader (New InputStreamReader ( system.in); try {//Break the data received by the keyboard into a character array char[] Chnumarr = In.readline (). ToCharArray ();//convert Arabic numerals to Chinese uppercase digits string[] Convertnum = new String[chnumarr.length];for (int x = 0; x < chnumarr.length; + +) {int num = Integer.parseint (chnumarr[ x]+ ""); convertnum[x] = Cnnum[num];} Embed the currency unit into the converted Chinese capital number to StringBuilder container = new StringBuilder ("Round"); for (int x = convertnum.length-1, y = 0; x >=0; X--, y++) {String num =Convertnum[x];if (Num.equals ("0")) {//To determine if the digits are thousand trillion positions, is the insertion unit if (y = = 4 | | y = = 8) container.insert (0, num+unit[y]); Elsecontainer.insert (0, num);} else {container.insert (0, num+unit[y]);}} Format fix string put = container.tostring ();p ut = put.replaceall ("Bai 0 + million", "Bai Wan");p ut = put.replaceall ("Bai 0 + billion", "Bai billion");p ut = PUT.R Eplaceall ("Thousand 0 + million", "Thousand Million");p ut = put.replaceall ("Thousand 0 + billion", "Thousand Billion");p ut = Put.replaceall ("0 +", "0");p ut = Put.replaceall ("0", "zero ");p ut = Put.replaceall (" titbits "," pickup ");p UT = Put.replaceall (" 0 "," circle "), or//after completion of the PrintOut System.out.println (" The amount is capitalized as: "+put);} catch (IOException e) {e.printstacktrace ();} Because it is the keyboard input, the stream will be disconnected after receiving the data, so there is no need to call the method of the Shutdown Resource}}
Still in the study, writing may not be very good.


Java Amount Conversion _ Arabic numerals into Chinese traditional form

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.