Wang of numbers to uppercase

Source: Internet
Author: User
Tags modulus

Usage scenario: You usually need to fill in uppercase numbers when writing regular amounts.

Implementation function: Digital to capitalize the main implementation: input lowercase Arabic numerals, converted to uppercase

Data example: input, 12345678.90 output, RMB: RMB ba Yuan, Qian Bai

Feature implementation:

HTML code

<Divclass= "Input-group">    <inputname= "KW"type= "text"class= "Form-control"placeholder= "Number"ID= "Lower"value= "12345678.90">    <Divclass= "Input-group-btn">    <Buttontype= "Submit"class= "Btn btn-success"ID= "Convert">Transformation</Button>        </Div></Div>

JS Code

<script src= "/wxp123/public/js/number/numberconvert.js" ></script> <script type= "Text/javascript" >                  $(function(){                      $("#convert"). Click (function() {Lower= $ ("#lower"). Val (); Upper=convertcurrency (lower); $("#upper"). val (upper);                      }); $("#lower"). Change (function() {Lower= $ ("#lower"). Val (); Upper=convertcurrency (lower); $("#upper"). val (upper);                  });               }); </script>

Numberconvert.js

functionconvertcurrency (currencydigits) {//Constants:    varMaximum_number = 99999999999.99; //predefine the radix characters and currency symbols for output:    varCn_zero = "0"; varCn_one = "One"; varCn_two = "Ii."; varCn_three = "three"; varCn_four = "The restaurant"; varcn_five = "WU"; varCn_six = "Lu"; varCn_seven = "Qi"; varCn_eight = "ba"; varCn_nine = "JIU"; varCn_ten = "Pick Up"; varcn_hundred = "Bai"; varCn_thousand = "thousand"; varCn_ten_thousand = "Million"; varCn_hundred_million = "billion"; varCn_symbol = "RMB:"; varCn_dollar = "Yuan"; varcn_ten_cent = "angle"; varcn_cent = "min"; varCn_integer = "whole"; //Variables:    varIntegral//represent integral part of digit number.    varDecimal//represent decimal part of digit number.    varOutputcharacters;//The output result.    varparts; vardigits, radices, bigradices, decimals; varZerocount; varI, p, D; varquotient, modulus; //Validate Input string:CurrencyDigits =currencydigits.tostring (); if(CurrencyDigits = = "") {alert ("Please enter the lowercase amount!" "); return""; }     if(Currencydigits.match (/[^,.\d]/)! =NULL) {alert ("Lowercase amount contains invalid characters!" "); return""; }     if((currencydigits). Match (/^ ((\d{1,3} (, \d{3}) * (. ( (\d{3},) *\d{1,3}))?) | (\d+ (. \d+)?)) $/) ==NULL) {alert ("The lowercase amount is not formatted correctly!" "); return""; }      //Normalize The format of input digits:CurrencyDigits = Currencydigits.replace (/,/g, "");//Remove comma delimiters.CurrencyDigits = Currencydigits.replace (/^0+/, "");//Trim Zeros at the beginning.    //Assert the number is not greater than the maximum number.    if(Number (CurrencyDigits) >Maximum_number) {Alert ("The amount is too large, should be less than 100 billion yuan!" "); return""; }      //Process the coversion from currency digits to characters:    //separate integral and decimal parts before processing coversion:Parts = Currencydigits.split (".")); if(Parts.length > 1) {Integral= Parts[0]; Decimal= Parts[1]; //Cut down redundant decimal digits that is after the second.decimal = Decimal.substr (0, 2); }     Else{integral= Parts[0]; Decimal= ""; }     //Prepare the characters corresponding to the digits:digits =NewArray (Cn_zero, Cn_one, Cn_two, Cn_three, Cn_four, Cn_five, Cn_six, Cn_seven, Cn_eight, cn_nine); Radices=NewArray ("", Cn_ten, cn_hundred, Cn_thousand); Bigradices=NewArray ("", Cn_ten_thousand, cn_hundred_million); Decimals=NewArray (cn_ten_cent, cn_cent); //Start Processing:Outputcharacters = ""; //Process Integral part if it is larger than 0:    if(Number (integral) > 0) {Zerocount= 0;  for(i = 0; i < integral.length; i++) {p= Integral.length-i-1; D= Integral.substr (i, 1); Quotient= P/4; Modulus= p% 4; if(d = = "0") {Zerocount++; }             Else {                 if(Zerocount > 0) {outputcharacters+ = Digits[0]; } Zerocount= 0; Outputcharacters+ = Digits[number (d)] +Radices[modulus]; }             if(modulus = = 0 && Zerocount < 4) {outputcharacters+=Bigradices[quotient]; Zerocount= 0; }} outputcharacters+=Cn_dollar; }     //Process Decimal Part if there is:    if(Decimal! = "") {          for(i = 0; i < decimal.length; i++) {D= Decimal.substr (i, 1); if(d! = "0") {outputcharacters+ = Digits[number (d)] +Decimals[i]; }         }     }     //Confirm and return the final output string:    if(Outputcharacters = = "") {outputcharacters= Cn_zero +Cn_dollar; }     if(Decimal = = "") {outputcharacters+=Cn_integer; } outputcharacters= Cn_symbol +outputcharacters; returnOutputcharacters;} 

--------------------------------------------------------------------------------------------------------------- -----------

Do not love to talk, everything to code-based, other all by perception!

Digital to Capital function address: http://www.wxp123.me/Index/detail/id/10.html

Wang Personal blog Address: http://blog.wxp123.me/

Wang of numbers to uppercase

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.