js 中处理金额数据 四舍五入数据(,处理和保留小数处理)
来源:互联网 发布:杀马特火星文 知乎 编辑:程序博客网 时间:2024/06/05 06:43
//方法名:任意大于等于0的数—num 例:11111.11 返回为:**,***.**形式数据 11,111.11 等
function toThousandsbuliu(num) {
// 判断是0 直接返回0.00
if(num == null){
return '0.00';
}
// 判断是0 直接返回0.00
if(num == null){
return '0.00';
}
//任意数字保留两位小数
var aa = Number(num).toFixed(2);
//
var num = (aa || 0).toString(), re = /\d{3}$/, result = '';
while ( re.test(num) ) {
result = RegExp.lastMatch + result;
if (num !== RegExp.lastMatch) {
result = ',' + result;
num = RegExp.leftContext;
} else {
num = '';
break;
}
}
if (num) { result = num + result; }
var sss = formatNumbuliu(result);
var aa = Number(num).toFixed(2);
//
var num = (aa || 0).toString(), re = /\d{3}$/, result = '';
while ( re.test(num) ) {
result = RegExp.lastMatch + result;
if (num !== RegExp.lastMatch) {
result = ',' + result;
num = RegExp.leftContext;
} else {
num = '';
break;
}
}
if (num) { result = num + result; }
var sss = formatNumbuliu(result);
return sss;
}
}
// 上边方法中嵌套的一个方法
function formatNumbuliu(str){
var newStr = "";
var count = 0;
if(str.indexOf(".")==-1){
for(var i=str.length-1;i>=0;i--){
if(count % 3 == 0 && count != 0){
newStr = str.charAt(i) + "," + newStr;
}else{
newStr = str.charAt(i) + newStr;
}
count++;
}
str = newStr + ".00"; //自动补小数点后两位
return str
}
else
{
for(var i = str.indexOf(".")-1;i>=0;i--){
if(count % 3 == 0 && count != 0){
newStr = str.charAt(i) + "," + newStr;
}else{
newStr = str.charAt(i) + newStr; //逐个字符相接起来
}
count++;
}
str = newStr + (str + "00").substr((str + "00").indexOf("."),3);
return str
}
var newStr = "";
var count = 0;
if(str.indexOf(".")==-1){
for(var i=str.length-1;i>=0;i--){
if(count % 3 == 0 && count != 0){
newStr = str.charAt(i) + "," + newStr;
}else{
newStr = str.charAt(i) + newStr;
}
count++;
}
str = newStr + ".00"; //自动补小数点后两位
return str
}
else
{
for(var i = str.indexOf(".")-1;i>=0;i--){
if(count % 3 == 0 && count != 0){
newStr = str.charAt(i) + "," + newStr;
}else{
newStr = str.charAt(i) + newStr; //逐个字符相接起来
}
count++;
}
str = newStr + (str + "00").substr((str + "00").indexOf("."),3);
return str
}
}
阅读全文
0 0
- js 中处理金额数据 四舍五入数据(,处理和保留小数处理)
- js中Double类型数据保留两位小数(非四舍五入)
- 对小数四舍五入处理
- 小数处理,四舍五入
- IOS 小数处理 四舍五入
- js金额数字格式化实现代码(三位加逗号处理保留两位置小数)
- 日期处理和金额处理js
- JS处理四舍五入
- JS四舍五入问题处理
- js四舍五入处理小数位
- 保留两位小数处理
- js处理金额
- js金额处理工具
- JS中浮点数四舍五入的处理
- freemarker中的小数处理,BigDecimal小数保留
- js 处理表格数据
- JS处理JSON数据
- js 处理json 数据
- 使用数据库UNIQUE constraint failed
- Spring MVC与Struts2的区别
- 结合FlexboxLayout实现标签选择功能
- ionic3(一)安装和配置
- php判断密码强度
- js 中处理金额数据 四舍五入数据(,处理和保留小数处理)
- svn插件集成myeclipse2014的两种方式
- Codeforces 834B-The Festive Evening
- java实现点击的图片验证码
- Raspberry Pi 3B I2C连接MPU-6050六轴感测器
- storyboard Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]
- flume高并发优化——(16)解决offsets变小问题
- webservice之cxf服务端及客户端实现(maven创建web项目)
- Linux的JCPU与PCPU区别