国产探花免费观看_亚洲丰满少妇自慰呻吟_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, "

主站蜘蛛池模板:
虞城县|
云阳县|
安阳县|
安康市|
赤水市|
通许县|
友谊县|
田阳县|
甘洛县|
筠连县|
水城县|
台北市|
中西区|
昌邑市|
西丰县|
义马市|
杭锦后旗|
卢龙县|
黄大仙区|
大荔县|
苏尼特左旗|
石狮市|
白玉县|
壤塘县|
会理县|
涞水县|
宁津县|
南宁市|
临洮县|
西丰县|
敖汉旗|
闽侯县|
景宁|
阳泉市|
龙游县|
临猗县|
福州市|
夏邑县|
从化市|
澄迈县|
称多县|