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

JavaScript 是否考慮本地小數分隔符?

Does JavaScript take local decimal separators into account?(JavaScript 是否考慮本地小數分隔符?)
本文介紹了JavaScript 是否考慮本地小數分隔符?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個以用戶本地化格式顯示小數的網頁,如下所示:

I've got a web page that displays decimals in a user's localized format, like so:

  • 英文:7.75
  • 荷蘭語:7,75

如果我在我的機器上的 JavaScript 中將兩個數字變量一起添加(其中數字取自上述格式的字符串),我會得到以下結果:

If I add two number variables together in JavaScript on my machine (where the numbers are taken from strings in the above formats) I get the following results:

  • 英文:7.75 + 7.75 = 15.5
  • 荷蘭語:7,75 + 7,75 = 0

如果我要在荷蘭用戶機器上運行此代碼,我是否應該期望英語格式的添加返回 0,而荷蘭語格式的添加返回 15,5?

If I was to run this code on a Dutch users machine, should I expect the English-formatted addition to return 0, and the Dutch-formatted addition to return 15,5?

簡而言之:JavaScript 計算是否在其字符串到數字的轉換中使用本地小數分隔符?

In short: Does the JavaScript calculation use local decimal separators in its string to number conversions?

推薦答案

不,分隔符在 javascript Number 中始終是點 (.).所以 7,75 的計算結果為 75,因為 , 調用從左到右的計算(在控制臺中嘗試:x=1,x+=1,alert(x) 或更多的點 var x=(7,75); alert(x);).如果你想轉換一個荷蘭語(嗯,不僅僅是荷蘭語,比如說 Continental European)格式的值,它應該是一個 String.您可以為 String 原型編寫擴展,例如:

No, the separator is always a dot (.) in a javascript Number. So 7,75 evaluates to 75, because a , invokes left to right evaluation (try it in a console: x=1,x+=1,alert(x), or more to the point var x=(7,75); alert(x);). If you want to convert a Dutch (well, not only Dutch, let's say Continental European) formatted value, it should be a String. You could write an extension to the String prototype, something like:

String.prototype.toFloat = function(){
      return parseFloat(this.replace(/,(d+)$/,'.$1'));
};
//usage
'7,75'.toFloat()+'7,75'.toFloat(); //=> 15.5

注意,如果瀏覽器支持,你可以使用 Number.toLocaleString

Note, if the browser supports it you can use Number.toLocaleString

console.log((3.32).toLocaleString("nl-NL"));
console.log((3.32).toLocaleString("en-UK"));

.as-console-wrapper { top: 0; max-height: 100% !important; }

這篇關于JavaScript 是否考慮本地小數分隔符?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 亚洲精品二区 | 99久久夜色精品国产亚洲96 | 超碰8| 久久久久久中文字幕 | 亚洲精品九九 | 波多野结衣精品 | 看片91| 成人三区四区 | 欧美a√| 国产欧美日韩精品一区 | 国产色在线 | 日韩av一区二区在线观看 | 国产偷自视频区视频 | 国产一区2区 | 国产在线观看 | 成人伊人| av片网| 国产精品日产欧美久久久久 | 欧美一区二不卡视频 | 嫩草黄色影院 | 日本一区二区三区视频在线 | 亚洲精品在线免费观看视频 | 日韩欧美在线观看 | 成人网视频 | 国产成人免费在线 | 天天久 | 天天精品综合 | 精品国产鲁一鲁一区二区张丽 | 污污免费网站 | 91免费观看国产 | 久久久久久国产精品免费免费狐狸 | 99热精品久久| 亚洲一二三区不卡 | 精品国产一区二区在线 | 美女爽到呻吟久久久久 | 国产精品不卡视频 | 中文无码日韩欧 | 日本免费视频在线观看 | 精品成人在线视频 | 中文字幕爱爱视频 | 久久久久久蜜桃一区二区 |