您的位置:首页 > 其它

accounting.formatNumber/Money的使用格式化货币、数字

2014-10-14 09:17 381 查看
之前写项目在jsp页面中一直使用jstl的<fmt>标签,但是在js/jqery使用不了。于是我又找到另外一种js插件accounting.js

首先使用accounting.js应先导入<script type="text/javascript" src="../../scripts/accounting.js"></script>

根据自己的文件位置填写src="×××/accounting.js".

然后是在使用的时候:accounting.formatMoney();accounting.formatNumber();

效果如下:

accounting.formatNumber(50000)-----50,000

accounting.formatMoney(5000)------$50,000.00

那为什么会出现这样的效果呢,这就需要查看accounting.js是怎么编写的。

首先,打开accounting.js我们会看到,如下:

lib.settings = {

currency: {

symbol : "",
// default currency symbol is '$'

//默认的货币符号
'$'

format : "%s%v",
// controls output: %s = symbol, %v = value (can be object, see docs)

//输出控制: %s = 符号, %v = 值或数字 (can be object: see below)

decimal : ".",
// decimal point separator

//小数点分隔符

thousand : ",",
// thousands separator,千位分隔符

precision : 0,
// decimal places,小数位数

grouping : 3
// digit grouping (not implemented yet),数字分组

},

number: {

precision : 0,
// default precision on numbers is 0,精度,默认的精度值为0

grouping : 3,
// digit grouping (not implemented yet),数字分组

thousand : ",",

decimal : "."

}

};

那么,这就是accounting.js要显示的样式了

如果想要更改显示样式直接在代码中这样写:

显示两位小数:accounting.formatNumber(50000,2)-------------50,000.00

显示中文货币符号:accounting.formatMoney(50000,"¥",2)-------¥50,000.00

不显示货币符号:accounting.formatMoney(50000,"",2)-----------50,000.00

ps:括号内直接传值,例如:accounting.formatMoney(gponGroups[i].baseFee,"",2),主要与<fmt>标签不同的是,可以在js/jquey中使用。

其他效果可以尝试一下,就不再写了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: