JS implements the uppercase conversion function of the amount

function transform(tranvalue) {
	try {
		var i = 1;
		var dw2 = new Array("", "million", "billion"); //large unit
		var dw1 = new Array("picks", "hundreds", "thousands"); //small unit
		var dw = new Array("zero", "one", "two", "three", "four", "wu", "lu", "qi", "e", "nine"); //integer Partially used
		//The following is converted from lowercase to uppercase and displayed in the total uppercase text box     
		//separate integers and decimals
		var source = splits (tranvalue);
		var num = source [0];
		var dig = source [1];
		//Convert the integer part
		var k1 = 0; // small unit
		var k2 = 0; // the major unit
		var sum = 0;
		var str = "";
		var len = source[0].length; //The length of the integer
		for(i = 1; i <= len; i++) {
			var n = source[0].charAt(len - i); //Get the number on a certain digit
			var bn = 0;
			if(len - i - 1 >= 0) {
				bn = source[0].charAt(len - i - 1); //Get the number on the previous digit
			}
			sum = sum + Number(n);
			if(sum != 0) {

				str = dw[Number(n)].concat(str); //Get the uppercase number corresponding to the number and insert it in front of the str string
				if(n == '0') sum = 0;
			}
			if(len - i - 1 >= 0) { //in the range of numbers
				if(k1 != 3) { //Add small unit
					if(bn != 0) {
						str = dw1[k1].concat(str);
					}
					k1 ++;
				} else { //Do not add small unit, increase unit
					k1 = 0;
					var temp = str.charAt (0);
					if(temp == "10,000" || temp == "100 million") //If there is no number before the large unit, discard the large unit
						str = str.substr(1, str.length - 1);
					str = dw2[k2].concat(str);
					sum = 0;
				}
			}
			if(k1 == 3) //Small unit to one thousand, then one large unit
			{
				k2++;
			}
		}
		//Convert the fractional part
		var strdig = "";
		if(dig != "") {
			var n = dig.charAt (0);
			if(n != 0) {
				strdig += dw[Number(n)] + "angle"; //add numbers
			}
			var n = dig.charAt (1);
			if(n != 0) {
				strdig += dw[Number(n)] + "minutes"; //add numbers
			}
		}
		str += "元" + strdig;
	} catch(e) {
		return "Incorrect input";
	}
	return str;
}
// split integers and decimals
function splits(tranvalue) {
	var value = new Array('', '');
	temp = tranvalue.split(".");
	for(var i = 0; i < temp.length; i++) {
		value[i] = temp[i];
	}
	return value;
}
console.info(transform('96354548503.4585345'));
console.info(transform('888.00'));
console.info(transform('789.0124'));
console.info(transform('kkk'));

Effect picture:

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326984784&siteId=291194637