Convert a number to a js function of the Renminbi expression, expression js

Source: Internet
Author: User

Convert a number to a js function of the Renminbi expression, expression js

There are many ways to convert numbers into large-sized RMB. This example describes how to use js to complete the conversion. Let's take a look at the following implementation code.

Function changeNumMoneyToChinese (money) {var cnNums = new Array ("zero", "one", "two", "three", "Si", "Wu", "Lu ", "identifier", "identifier", "identifier"); // var cnIntRadice = new Array ("", "Pick", "identifier", "identifier "); // basic unit var cnIntUnits = new Array ("", "MB "); // The expansion unit of the integer part var cnDecUnits = new Array ("", "points", "", ""); // The unit of the corresponding fractional part var cnInteger = "integer"; // The character var cnIntLast = "Yuan" followed by the integer amount "; // unit var maxNum = 999999999999999.9999 after Integer type ;/ /The largest processed number var IntegerNum; // The amount integer part var DecimalNum; // The amount fractional part var ChineseStr = ""; // The output Chinese amount string var parts; // The array used to separate the amount, pre-defined if (money = "") {return "";} money = parseFloat (money); if (money> = maxNum) {alert ('processing Max number'); return "" ;}if (money = 0) {ChineseStr = cnNums [0] + cnIntLast + cnInteger; return ChineseStr ;} money = money. toString (); // convert to string if (money. indexOf (". ") =-1) {IntegerNum = Money; DecimalNum = '';} else {parts = money. split (". "); IntegerNum = parts [0]; DecimalNum = parts [1]. substr (0, 4);} if (parseInt (IntegerNum, 10)> 0) {// obtain the integer part conversion var zeroCount = 0; var IntLen = IntegerNum. length; for (var I = 0; I <IntLen; I ++) {var n = IntegerNum. substr (I, 1); var p = IntLen-I-1; var q = p/4; var m = p % 4; if (n = "0 ") {zeroCount ++;} else {if (zeroCount> 0 ){ ChineseStr + = cnNums [0];} zeroCount = 0; // return ChineseStr + = cnNums [parseInt (n)] + cnIntRadice [m];} if (m = 0 & zeroCount <4) {ChineseStr + = cnIntUnits [q] ;}} ChineseStr + = cnIntLast; // integer part processed} if (DecimalNum! = '') {// Var decLen = DecimalNum. length; for (var I = 0; I <decLen; I ++) {var n = DecimalNum. substr (I, 1); if (n! = '0') {ChineseStr + = cnNums [Number (n)] + cnDecUnits [I] ;}} if (ChineseStr = '') {ChineseStr + = cnNums [0] + cnIntLast + cnInteger;} else if (DecimalNum = '') {ChineseStr + = cnInteger;} return ChineseStr ;}

In JavaScript, how do I convert money of the numeric type to uppercase?

Function Chinese (num )//
{
If (! /^ \ D * (\. \ d *)? $/. Test (num) {alert ("Number is wrong! "); Return" Number is wrong! ";}

Var AA = new Array ("zero", "one", "two", "three", "Si", "Wu", "Lu", "Lu ", "success", "fail ");
Var BB = new Array ("circle", "Pick", "Hour", "Hour", "point ","");

Var a = ("" + num ). replace (/(^ 0 *)/g ,""). split (". "), k = 0, re = "";

For (var I = a [0]. length-1; I> = 0; I --)
{
Switch (k)
{
Case 0: re = BB [7] + re; break;
Case 4: if (! New RegExp ("0 {4} \ d {" + (a [0]. length-i-1) + "} $"). test (a [0])
Re = BB [4] + re; break;
Case 8: re = BB [5] + re; BB [7] = BB [5]; k = 0; break;
}
If (k % 4 = 2 & a [0]. charAt (I + 2 )! = 0 & a [0]. charAt (I + 1) = 0) re = AA [0] + re;
If (a [0]. charAt (I )! = 0) re = AA [a [0]. charAt (I)] + BB [k % 4] + re; k ++;
}

... The remaining full text>

Javascript numbers are converted to uppercase in lowercase.

<Script type = "text/javascript">
// Main Function
Function DX (n ){
If (! /^ (0 | [1-9] \ d *) (\. \ d + )? $/. Test (n ))
Return "invalid data ";
Var unit = "hundreds of thousands of yuan points", str = "";
N + = "00 ";
Var p = n. indexOf ('.');
If (p> = 0)
N = n. substring (0, p) + n. substr (p + 1, 2 );
Unit = unit. substr (unit. length-n. length );
For (var I = 0; I <n. length; I ++)
Str + = 'zero character (I) + unit. charAt (n. charAt (I );
Return str. replace (/0 (Thousands | hundreds | pick | corners)/g, "zero "). replace (/(zero) +/g, "zero "). replace (/0 (| RMB)/g, "$1 "). replace (/(million) | 1 (pick up)/g, "$1 $2 "). replace (/^ RMB 0? | Zero/g, ""). replace (/Yuan $/g, "Yuan whole ");
}
// The following is the test output.
Alert ('1970. 56' + DX (1526365 ))
</Script>

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.