1、我们首先模拟个数据~vue:data( ){ reutrn:{ num:10000 } } 原生js:var num=10000
2、在项目中创建一个.js文件写入js方法,如下;
3、js使用可以直接写在方法外
4、vue可以将.js文件引入main文件,或者在本页面引入
5、写入要转换的变量值
<!DOCTYPE html><html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script>
function change(num){
num = parseFloat(num)
if (isNaN(num))
{
alert('不是数字')
return false
}else{
var newnum=""
num=num+""
var temp=num.split("\.")
var mylength=0
if(temp.length>=2){
mylength=temp[1].length
}
switch(mylength){
case 0:
newnum=num+".00"
break
case 1:
newnum=num+"0"
break
default:
newnum=num
break
}
alert("新数字是"+newnum)
return newnum
}
}
function test(){
var num=document.getElementById('num').value
change(num)
}
</script>
</head>
<body>
<input type="text" value="" id="num"/><input type="button" onclick="test()" value="测试"/>
</body>
</html>
numFormat(num) {
var c = (num.toString().indexOf ('.') !== -1) ? num.toLocaleString() : num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
return c
}
console.log(this.numFormat(20181029.120))
//格式化钱数,三位加一逗号
function toThousands(number) {
number = number +""
number = number.replace(/\,/g, "")
if(isNaN(number) || number == "")return ""
number = Math.round(number * 100) / 100
if (number <0)
return '-' + outputdollars(Math.floor(Math.abs(number) - 0) + '') + outputcents(Math.abs(number) - 0)
else
return outputdollars(Math.floor(number - 0) + '') + outputcents(number - 0)
}
//格式化金额
function outputdollars(number) {
if (number.length <= 3)
return (number == '' ? '0' : number)
else {
var mod = number.length % 3
var output = (mod == 0 ? '' : (number.substring(0, mod)))
for (i = 0i <Math.floor(number.length / 3)i++) {
if ((mod == 0) &&(i == 0))
output += number.substring(mod + 3 * i, mod + 3 * i + 3)
else
output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3)
}
return (output)
}
}
function outputcents(amount) {
amount = Math.round(((amount) - Math.floor(amount)) * 100)
return (amount <10 ? '.0' + amount : '.' + amount)
}