Preparation before writing
Below are the key knowledge points needed to write this method:
1. charAt(index): Returns the character at the specified position.
2. substr(start, length): Returns a string of a specified length starting from a specified position.
Parameters:
start:Required. The starting position of the string to be extracted.
length:Optional. The number of characters included in the returned string. When length <= 0, an empty string is returned; when length is not specified, the extracted string extends from start to the end of the string.
3. substring(start, end): Returns the specified substring.
Parameters:
start:Indicates the starting position of the substring; the index is counted from 0.
end:Indicates the ending position of the substring; the index is counted from 0.
Special cases:
If only one parameter is provided, it is set as the starting position by default, and the characters from that parameter to the end of the string are returned.
Between start and end, the smaller one is used as the starting position and the larger one as the ending position. For example, a.substring(1, 5) and a.substring(5, 1) return the same string.
If start or end is NaN or negative, it is set to 0.
Specific code
function ToString(n) {
if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)){
return "数据非法"; //判断数据是否大于0
}
var unit = "千百拾亿千百拾万千百拾元角分", str = "";
n += "00";
var indexpoint = n.indexOf('.'); // 如果是小数,截取小数点前面的位数
if (indexpoint >= 0){
n = n.substring(0, indexpoint) + n.substr(indexpoint+1, 2); // 若为小数,截取需要使用的unit单位
}
unit = unit.substr(unit.length - n.length); // 若为整数,截取需要使用的unit单位
for (var i=0; i < n.length; i++){
str += "零壹贰叁肆伍陆柒捌玖".charAt(n.charAt(i)) + unit.charAt(i); //遍历转化为大写的数字
}
return str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整"); // 替换掉数字里面的零字符,得到结果
}
Online test
Author nickname: Lichee Lychee
Author email: 442070894@qq.com