码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript字符串技巧

时间:2015-01-25 17:54:28      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

1.使用逗号分割金额
String.prototype.strReverse = function() {
    return this.split(‘‘).reverse().join(‘‘);
}
function amountSplit(amount) {
    return amount.toString().strReverse().replace( /(\d+\.)?(\d{1,3})/g, ‘$1$2,‘ ).strReverse().substring( 1 );
}
console.log( amountSplit( 120000.12 ) ); // 120,000.12
console.log( amountSplit( 1285000) ); // 1,285,000

 

2.将小数转换为百分比
Math.round(num1 / num2 * 10000) / 100.00 + ‘%‘

 

JavaScript字符串技巧

标签:

原文地址:http://www.cnblogs.com/happyfreelife/p/4248455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!