C # convert Chinese characters to Arabic numerals

Source: Internet
Author: User

I encountered the requirement to convert Chinese characters and numbers to Arabic numerals. I checked on the Internet and found that all Arabic numerals are converted into Chinese characters, I had to write it myself. After studying the algorithm , I finally completed the function. The principle is also very simple: cyclically input each bit of the string to determine whether it is a unit or a number. If it is a unit, it is saved first. If it is a number, it is multiplied by the previous unit and then saved to the result, if you do not understand the result, you can see the Code comment.

/// <Summary> /// convert the number /// </Summary> protected static long chartonumber (char c) {Switch (c) {Case '1': return 1; case '2': return 2; Case '3': return 3; Case '4': return 4; Case '5': return 5; Case '6': return 6; case '7': Return 7; Case '8': Return 8; Case '9': Return 9; Case '0': Return 0; default: Return-1 ;}} /// <summary> /// conversion unit // </Summary> protected static long chartounit (char c) {Switch (c) {Case '10': return 10; case '100': return 100; Case '100': Return 1000; Case '100': Return 10000; Case '66': Return 100000000; default: return 1 ;}} /// <summary> // convert Chinese numbers to Arabic numerals // </Summary> // <Param name = "cnum"> Chinese numerals </param>/ // <returns> long integer Arabic numerals </returns> Public static long parsecntoint (string cnum) {cnum = RegEx. replace (cnum, "\ s +", ""); long firstunit = 1; // Level 1 unit long secondunit = 1; // Level 2 unit long tmpunit = 1; // temporary Unit variable long result = 0; // result for (INT I = cnum. length-1; I>-1; -- I) // process {tmpunit = chartounit (cnum [I]) in sequence from low to high. // retrieve the unit of this bit if (tmpunit> firstunit) // If You Want To determine whether this bit is a number or a unit {firstunit = tmpunit; // If yes, the value is assigned, for the next cycle, use secondunit = 1; if (I = 0) // process {result + = firstunit * secondunit;} starting with "10", "11 ;} continue; // end this loop} else if (tmpunit> secondunit) {secondunit = tmpunit; continue;} result + = firstunit * secondunit * chartonumber (cnum [I]); // if it is a number, multiply it with the Unit and save it to the result} return result ;}

the result of executing parsecntoint ("one thousand two hundred and thirty-five") is 1235

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.