久久久久久久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?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 国产在线第一页 | 四虎永久免费地址 | 欧美婷婷 | 午夜精品一区二区三区在线视频 | 国产精品一区二区三区久久 | 国产亚洲欧美日韩精品一区二区三区 | 99re在线视频观看 | 国产精品综合一区二区 | 久久精品16 | 成人欧美一区二区三区视频xxx | 久久精品国产99国产 | 国产91精品久久久久久久网曝门 | 一区二区三区高清不卡 | 免费视频二区 | 超碰在线免费公开 | 天天综合国产 | 亚洲高清视频一区二区 | 毛片免费看 | 男女网站在线观看 | 超碰在线免费av | 亚洲精品久久久 | 天天天天天天天干 | 国产在线精品一区二区三区 | 国产一区二区免费 | 亚洲精品福利视频 | 免费看国产一级特黄aaaa大片 | 91精品国产91久久久久久最新 | 午夜成人在线视频 | 欧美一区二区小视频 | 中文在线一区二区 | 337p日本欧洲亚洲大胆鲁鲁 | 在线观看中文字幕 | 亚洲欧美日本在线 | 综合另类 | 久久新| 亚洲精品一区二区在线观看 | 精品国产乱码久久久久久丨区2区 | 国产精品久久久久久久久久免费看 | 亚洲成人一区 | 欧美一区二区三区一在线观看 | 欧美一级片在线看 |