2017-08-10 31 views
1

在爲值格式化程序服務編寫測試時,我注意到有一些關於Number.prototype.toLocaleString("sv-SE")的問題。如何正確聲明由Number.prototype.toLocaleString(「sv-SE」)返回的值?

let myValue = (100000).toLocaleString("sv-SE"); 
 

 
console.log(typeof myValue); 
 
console.log(myValue); 
 
console.log(myValue === "100 000"); // false, why?

我希望最後的日誌語句返回true。但它不,爲什麼?

+1

即字符是[U + 00A0(NO-BREAK SPACE)](http://www.fileformat.info/info/統一字符編碼/炭/ 00A0/index.htm的)。不是正常的空格字符([U + 0020](http://www.fileformat.info/info/unicode/char/0020/index.htm))。 – Phylogenesis

+0

對,明白了,只需要使用正確的空格字符。你不把這個作爲答案? –

回答

2

正如我在評論中提到的那樣,「空格字符」實際上是U+00A0 (NO-BREAK SPACE)。這個字符看起來像一個正常的空間,但不適用於行尾換行算法。

下面的代碼按預期方式工作:

let myValue = (100000).toLocaleString("sv-SE"); 
 

 
console.log(typeof myValue); 
 
console.log(myValue); 
 
console.log(myValue === "100\u00a0000");