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

有沒有辦法在 JavaScript 中通過 Intl.NumberFormat 反轉

Is there a way to reverse the formatting by Intl.NumberFormat in JavaScript(有沒有辦法在 JavaScript 中通過 Intl.NumberFormat 反轉格式)
本文介紹了有沒有辦法在 JavaScript 中通過 Intl.NumberFormat 反轉格式的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

The Intl.NumberFormat (see Mozilla's doc) provides a nice way in Javascript to format numbers into a current locale`s version like this:

new Intl.NumberFormat().format(3400); // returns "3.400" for German locale

But I couldn't find a way to reverse this formatting. Is there something like

new Intl.NumberFormat().unformat("3.400"); // returns 3400 for German locale

Thanks for any help.

解決方案

I have found a workaround:

/**
 * Parse a localized number to a float.
 * @param {string} stringNumber - the localized number
 * @param {string} locale - [optional] the locale that the number is represented in. Omit this parameter to use the current locale.
 */
function parseLocaleNumber(stringNumber, locale) {
    var thousandSeparator = Intl.NumberFormat(locale).format(11111).replace(/p{Number}/gu, '');
    var decimalSeparator = Intl.NumberFormat(locale).format(1.1).replace(/p{Number}/gu, '');

    return parseFloat(stringNumber
        .replace(new RegExp('\' + thousandSeparator, 'g'), '')
        .replace(new RegExp('\' + decimalSeparator), '.')
    );
}

Using it like this:

parseLocaleNumber('3.400,5', 'de');
parseLocaleNumber('3.400,5'); // or if you have German locale settings
// results in: 3400.5

Not the nicest solution but it works :-)

If anyone knows a better way of achieving this, feel free to post your answer.

Update

  • Wrapped in a complete reusable function
  • Using the regex class p{Number} to extract the separator. So that it also works with non-arabic digits.
  • Using number with 5 places to support languages where numbers are separated at every fourth digit.

這篇關于有沒有辦法在 JavaScript 中通過 Intl.NumberFormat 反轉格式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進程中創建子窗口時如何修復 BrowserWindow 不是構造函數錯誤) - IT屋-程序員軟件開發技術
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 上執行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲?)
How to access camera/webcamera inside electron app?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: www.一区二区三区 | 国产高清精品一区二区三区 | 超碰在线播 | av永久 | 精品真实国产乱文在线 | 91精品国产91久久综合桃花 | 97影院在线午夜 | 亚洲精品免费视频 | 日韩一级免费观看 | 综合中文字幕 | 久久免费高清视频 | 日韩aⅴ片 | 欧美日韩三区 | av中文网 | 日本不卡一区二区三区在线观看 | 欧美精品在线免费 | 免费在线黄色av | 色视频在线免费观看 | 日本在线综合 | 免费看a| 亚洲精品久久久久久久久久久 | 精品久久久久久久久久久久久久久久久 | 亚洲国产精品久久人人爱 | 成人精品一区二区三区 | 人人射人人插 | 国产一区三区在线 | 日韩高清一区 | 中文字幕日韩一区 | 三极网站 | 午夜视频免费在线观看 | 亚洲成年在线 | 久久精品亚洲一区 | 久久大 | 亚洲国产一区二区三区在线观看 | 亚洲高清在线 | 美女视频一区 | caoporn免费在线视频 | 色资源站 | 精品久久久久久亚洲精品 | 日本中文在线 | 青青草中文字幕 |