A JS function that converts a number to an uppercase renminbi expression

Source: Internet
Author: User
Tags expression

Convert the number to uppercase renminbi, there are many methods, this example is to use JS to complete, look at the following implementation code

function Changenummoneytochinese (Money) {

var cnnums = new Array ("0", "one", "II", "three", "Restaurant", "WU", "Lu", "Qi", "ba", "Nine"); Number of Chinese characters

var cnintradice = new Array ("", "Pick Up", "Bai", "Qian"); Unit

var cnintunits = new Array ("", "Million", "billion", "Mega"); Corresponding integer part expansion unit

var cndecunits = new Array ("Angle", "min", "Penny", "pct"); corresponding to the fractional part of the unit

var Cninteger = "whole"; The character following the integer amount

var cnintlast = "Yuan"; Units after the integral type

var maxnum = 999999999999999.9999; Maximum number of processed

var integernum; Amount of the whole number of parts

var decimalnum; Amount of small Parts

var chinesestr = ""; Chinese amount string for output

var parts; The array to use after separating the amount, predefined

if (Money = = "") {

Return "";

}

Money = parsefloat (money);

if (Money >= maxnum) {

Alert (' exceeds maximum processing 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) > 0) {//Get integral partial conversions

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 to zero

Chinesestr + = Cnnums[parseint (n)] + cnintradice[m];

}

if (m = = 0 && Zerocount < 4) {

Chinesestr + = Cnintunits[q];

}

}

Chinesestr + = Cnintlast;

Integral part processing complete

}

if (Decimalnum!= ') {//Decimal part

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;

}

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.