方法一
let num = 3333
num.toFixed(2).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g,'$&,')
方法二:
function formatNumber(number, pattern, bNoZero, nZeroLen) {
bNoZero = (eval(bNoZero) !== false);
switch (pattern) {
case 1:
case "JE_2":
pattern = '#,___.00'
break
case 2:
case "JE":
pattern = '#,___.00'
break
case 2:
case "SL":
pattern = '#,___#'
break
case "SL_1":
pattern = '#,___.0'
break
case "SL_2":
pattern='#___.00'
if((number+"")=="") return ""
break
case 3:
case "AREA" :
pattern = '#,___.000'
break
case "SELF":
return (number === 0 && bNoZero)? "" : number
break
case null:
pattern = "#.00#"
}
var nZeroLen = parseInt(nZeroLen || "0")
if (nZeroLen > 0 && pattern.indexOf('.') !== -1) {
var sTmpFormat = ""
var sFormatE = pattern.split('.')[1]
sFormatE = sFormatE.replace(/0/ig, '')
for (var i = 0; i < nZeroLen; i++) {
sTmpFormat += "0"
}
pattern = pattern.split('.')[0] + "." + sTmpFormat + sFormatE
}
var bNig = (number < 0)
number = Math.abs(number)
if (number === 0 && bNoZero) return ""
var str = number.toString()
var strInt
var strFloat
var formatInt
var formatFloat
if (/\./g.test(pattern)) {
formatInt = pattern.split('.')[0]
formatFloat = pattern.split('.')[1]
} else {
formatInt = pattern
formatFloat = null
}
if(/\./g.test(str)) {
if (formatFloat !== null) {
var tempFloat = Math.round(parseFloat('0.' + str.split('.')[1]) * Math.pow(10, formatFloat.length)) / Math.pow(10, formatFloat.length)
strInt = (Math.floor(number)+Math.floor(tempFloat)).toString()
strFloat = /\./g.test(tempFloat.toString()) ? tempFloat.toString().split('.')[1] : '0'
} else {
strInt = Math.round(number).toString()
strFloat = '0'
}
} else {
strInt = str
strFloat = '0'
}
if (formatInt !== null) {
var outputInt = ''
var zero = formatInt.match(/0*$/)[0].length
var comma = null
if (/,/g.test(formatInt)) {
comma = formatInt.match(/,[^,]*/)[0].length - 1
}
var newReg = new RegExp('(\\d{' + comma + '})', 'g')
if (strInt.length < zero) {
outputInt = new Array(zero + 1).join('0') + strInt;
outputInt = outputInt.substr(outputInt.length - zero, zero)
} else {
outputInt = strInt
}
var outputInt = outputInt.substr(0,outputInt.length%comma)+outputInt.substring(outputInt.length%comma).replace(newReg, (comma !== null ? ',' : '') + '$1')
outputInt = outputInt.replace(/^,/, '')
strInt = outputInt
}
if (formatFloat !== null) {
var outputFloat = ''
var zero = formatFloat.match(/^0*/)[0].length
if (strFloat.length < zero) {
outputFloat = strFloat + new Array(zero + 1).join('0')
var outputFloat1 = outputFloat.substring(0, zero)
var outputFloat2 = outputFloat.substring(zero,formatFloat.length)
outputFloat = outputFloat1 + outputFloat2.replace(/0*$/, '')
} else {
outputFloat = strFloat.substring(0, formatFloat.length)
}
strFloat = outputFloat
} else {
if (pattern !== '' || (pattern === '' && strFloat === '0')) {
strFloat = ''
}
}
var sData = strInt + (strFloat === '' ? '' : '.' + strFloat)
return (bNig ? "-" : "") + sData
}
alert(formatNumber(32323.444, "#,###.##"))
alert(formatNumber(32323.444, "JE"))
方法三:
export function numToZW(num) {
return numToRMB(num)
}
export function numToRMB(num) {
var str1 = '零壹贰叁肆伍陆柒捌玖'
var str2 = '万仟佰拾亿仟佰拾万仟佰拾元角分'
var str3
var str4
var str5 = ''
var i
var j
var ch1
var ch2
var nzero = 0
num = Math.abs(num).toFixed(2)
str4 = (num * 100).toFixed(0).toString()
j = str4.length
if (j > 15) { return '溢出' }
str2 = str2.substr(15 - j)
for (i = 0; i < j; i++) {
str3 = str4.substr(i, 1)
if (i !== (j - 3) && i !== (j - 7) && i !== (j - 11) && i !== (j - 15)) {
if (str3 === '0') {
ch1 = ''
ch2 = ''
nzero = nzero + 1
} else if (str3 !== '0' && nzero !== 0) {
ch1 = '零' + str1.substr(str3 * 1, 1)
ch2 = str2.substr(i, 1)
nzero = 0
} else {
ch1 = str1.substr(str3 * 1, 1)
ch2 = str2.substr(i, 1)
nzero = 0
}
} else {
if (str3 !== '0' && nzero !== 0) {
ch1 = '零' + str1.substr(str3 * 1, 1)
ch2 = str2.substr(i, 1)
nzero = 0
} else if (str3 !== '0' && nzero === 0) {
ch1 = str1.substr(str3 * 1, 1)
ch2 = str2.substr(i, 1)
nzero = 0
} else if (str3 === '0' && nzero >= 3) {
ch1 = ''
ch2 = ''
nzero = nzero + 1
} else {
if (j >= 11) {
ch1 = ''
nzero = nzero + 1
} else {
ch1 = ''
ch2 = str2.substr(i, 1)
nzero = nzero + 1
}
}
}
if (i === (j - 11) || i === (j - 3)) {
ch2 = str2.substr(i, 1)
}
str5 = str5 + ch1 + ch2
if (i === j - 1 && str3 === '0') {
str5 = str5 + '整'
}
}
if (num === 0) {
str5 = '零元整'
}
return str5
}