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