久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 JavaScript 格式化數(shù)字?

How to format numbers using JavaScript?(如何使用 JavaScript 格式化數(shù)字?)
本文介紹了如何使用 JavaScript 格式化數(shù)字?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想使用 javascript 格式化數(shù)字,如下所示:

I want to format number using javascript as below:

10.00=10,00 
1,000.00=1.000,00

推薦答案

每個瀏覽器都支持 Number.prototype.toLocaleString(),這是一種旨在從數(shù)字返回本地化字符串的方法.但是,規(guī)范將其定義如下:

Every browser supports Number.prototype.toLocaleString(), a method intended to return a localized string from a number. However, the specification defines it as follows:

生成一個字符串值,該值表示根據(jù)宿主環(huán)境當前語言環(huán)境的約定格式化的數(shù)字值.此函數(shù)依賴于實現(xiàn),允許但不鼓勵它返回與 toString 相同的內(nèi)容.

Produces a string value that represents the value of the Number formatted according to the conventions of the host environment's current locale. This function is implementation-dependent, and it is permissible, but not encouraged, for it to return the same thing as toString.

依賴于實現(xiàn)意味著由供應商決定結果的外觀,并導致互操作性問題.

Implementation-dependant means that it's up to the vendor how the result will look, and results in interoperability issues.

Internet Explorer(IE 5.5 到 IE 9)最接近您想要的格式,并以貨幣樣式格式化數(shù)字 - 千位分隔符并固定為小數(shù)點后 2 位.

Internet Explorer (IE 5.5 to IE 9) comes closest to what you want and formats the number in a currency style - thousands separator and fixed at 2 decimal places.

Firefox (2+) 使用千位分隔符和小數(shù)位格式化數(shù)字,但僅在適用的情況下.

Firefox (2+) formats the number with a thousands separator and decimal places but only if applicable.

Opera、Chrome 和Safari 輸出與 toString() 相同 -- 沒有千位分隔符,僅在需要時使用小數(shù)位.

Opera, Chrome & Safari output the same as toString() -- no thousands separator, decimal place only if required.

我想出了以下代碼(基于 我的一個舊答案) 嘗試將結果標準化以像 Internet Explorer 的方法一樣工作:

I came up with the following code (based on an old answer of mine) to try and normalize the results to work like Internet Explorer's method:

(function (old) {
    var dec = 0.12 .toLocaleString().charAt(1),
        tho = dec === "." ? "," : ".";

    if (1000 .toLocaleString() !== "1,000.00") {
        Number.prototype.toLocaleString = function () {
           var neg = this < 0,
               f = this.toFixed(2).slice(+neg);

           return (neg ? "-" : "") 
                  + f.slice(0,-3).replace(/(?=(?!^)(?:d{3})+(?!d))/g, tho) 
                  + dec + f.slice(-2);
        }
    }
})(Number.prototype.toLocaleString);

這將使用瀏覽器的內(nèi)置本地化(如果可用),同時在其他情況下優(yōu)雅地降級為瀏覽器的默認語言環(huán)境.

This will use the browser's built-in localization if it's available, whilst gracefully degrading to the browser's default locale in other cases.

工作演示:http://jsfiddle.net/R4DKn/49/

這篇關于如何使用 JavaScript 格式化數(shù)字?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進程中創(chuàng)建子窗口時如何修復 BrowserWindow 不是構造函數(shù)錯誤) - IT屋-程序員軟件開發(fā)技術
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個 loadURL 上執(zhí)行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內(nèi)使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲?)
How to access camera/webcamera inside electron app?(如何在電子應用程序中訪問相機/網(wǎng)絡攝像頭?)
主站蜘蛛池模板: av片网站| 精品久久久久久久久久 | 欧美日韩成人在线观看 | 天堂一区二区三区四区 | 久久久久资源 | 国产综合欧美 | 九色.com| 中文字幕人成人 | 久草视频网站 | 91精品亚洲| 欧洲一级黄 | 99精品亚洲国产精品久久不卡 | 亚洲91av| 永久av| 超碰国产在线 | 国产高清在线观看 | 亚州影院 | 欧美日韩一区二区视频在线观看 | 欧美日韩国产一区二区 | 成人精品系列 | 美国av片在线观看 | 国产女人第一次做爰毛片 | 婷婷色国产偷v国产偷v小说 | www久久久| 免费视频一区二区 | 国产精品欧美一区二区 | 日本免费在线 | 国产精品日产欧美久久久久 | 区一区二区三在线观看 | 成人在线视频免费观看 | 成人精品视频免费 | 欧美黄色一区 | 日韩免费网站 | 中文字幕一区二区三区日韩精品 | 午夜爽爽爽男女免费观看 | 日韩中文欧美 | 久久亚洲精品视频 | 成人午夜精品一区二区三区 | 国产伦精品一区二区三区精品视频 | 日韩欧美在线免费观看视频 | 亚洲一区|