国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Javascript將數字轉化成為貨幣格式字符串

2019-11-20 09:37:33
字體:
來源:轉載
供稿:網友

這里第一個方法是用JavaScript將數字number轉換為貨幣字符串的格式(參數:保留小數位數,貨幣符號,整數部分千位分隔符,小數分隔符)

這里第二個方法是用簡單的正則表達式將貨幣字符換轉換為純凈的數字字符串,之后便可以將字符串轉換為數字number
JavaScript Money Format(用prototype對Number進行擴展)

// Extend the default Number object with a formatMoney() method:// usage: someVar.formatMoney(decimalPlaces, symbol, thousandsSeparator, decimalSeparator)// defaults: (2, "$", ",", ".")Number.prototype.formatMoney = function (places, symbol, thousand, decimal) {  places = !isNaN(places = Math.abs(places)) ? places : 2;  symbol = symbol !== undefined ? symbol : "$";  thousand = thousand || ",";  decimal = decimal || ".";  var number = this,    negative = number < 0 ? "-" : "",    i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",    j = (j = i.length) > 3 ? j % 3 : 0;  return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(/d{3})(?=/d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");};

如下便是一些轉換實例:

// Default usage and custom precision/symbol :var revenue = 12345678;alert(revenue.formatMoney()); // $12,345,678.00alert(revenue.formatMoney(0, "HK$ ")); // HK$ 12,345,678// European formatting:var price = 4999.99;alert(price.formatMoney(2, "

主站蜘蛛池模板:
丰顺县|
丹凤县|
韶山市|
得荣县|
西充县|
湖北省|
五峰|
辰溪县|
阳高县|
仪陇县|
平安县|
云林县|
莫力|
锦州市|
北宁市|
新平|
东兰县|
天台县|
晋中市|
新民市|
深水埗区|
镇沅|
固镇县|
托克逊县|
建昌县|
皮山县|
喀什市|
和硕县|
廊坊市|
珲春市|
瑞丽市|
江达县|
安龙县|
麟游县|
镇雄县|
增城市|
沐川县|
新乐市|
四子王旗|
南阳市|
武宣县|