首頁>技術>

有許多JavaScript Number方法可以用於處理數字值。這些數字格式支援浮點數,整數,十進位制,十六進位制和符號。

在這裡我整理了一些方法可以幫助你提升開發效率,同時使操作和修改數值變得更加容易。具體方法見下面:

1、 Number()

該Number()方法可以將字串轉換為數字。

// Example 1let x = '10'let num = Number(x)console.log(num)                 // Output: 10console.log(num * 9)             // Output: 90// Example 2let x = truelet num = Number(x)console.log(num)                 // Output: 1console.log(num + 9)             // Output: 10//Example 3let x = falselet num = Number(x)console.log(num)                 // Output: 0console.log(num + 9)             // Output: 9

在上面的示例中,Number(value)方法用於將字串轉換x為整數並對數字值執行運算。設定x為true返回1並false返回0

2、 parseInt()

parseInt()與number()方法非常相似,將parseInt()字串格式化為整數。

// Example 1let x = '10.99'let num = parseInt(x)console.log(num)                 // Output: 10// Example 2let x = '7 days'let num = parseInt(x)console.log(num)                 // Output: 7// Example 3let x = 'day 7'let num = parseInt(x)console.log(num)                 // Output: NaN

該parseInt(value)方法採用一個字串x,一個十進位制數字並返回一個整數。它返回第一個整數7,值為“ 7 days”。如果我們將值更改x為“第7天”,則它將返回,NaN因為該方法找不到數字值。

3、parseFloat()

該parseFloat()方法解析一個字串值,並返回帶有其十進位制值的數字。

// Example 1let x = '10.99'let num = parseFloat(x)console.log(num)                 // Output: 10.99// Example 2let x = '2.49 3.99'let num = parseFloat(x)console.log(num)                 // Output: 2// Example 3let x = 'day 7'let num = parseFloat(x)console.log(num)                // Output: NaN
與相似parseInt(),parseFloat()檢查以查詢字串中的第一個字元是一個數字,如果是,它將解析該字串並將其返回為帶有十進位制值的數字。設定parseInt()和parseFloat()方法分開的主要原因是parseFloat()在解析字串之後,返回帶有其十進位制值的數字,而parseInt()僅返回整數。4、 toString()

該toString()方法將數值轉換為字串。

// Example 1let x = 10let num = x.toString()console.log(num)                // Output: '10'// Example 2let x = 10let num = x.toString(2)console.log(num)                // Output: 1010          

將數字2作為引數新增到toString()方法中,將返回數字的二進位制值。

5、 toExponential()

顧名思義,toExponential()將數字轉換為字串並以指數格式返回。

// Example 1let x = 456.789let num = x.toExponential()console.log(num)                 // Output: 4.56789e+2// Example 2let x = 456.789let num = x.toExponential(2)console.log(num)                 // Output: 4.57e+2
帶2位數字的引數,返回帶兩位十進位制數字的值。6、toFixed()

toFixed()方法將數字四捨五入到最接近的最高或最低定點符號。它帶有一個引數,該引數表示應在小數點後顯示位數。

// Example 1var num = 4.56789;console.log(num.toFixed())             // Output : 5// Example 2var num = 4.56789;console.log(num.toFixed(2))            // Output : 4.57
在上面的示例中,該toFixed(digits)方法使用小數點後的2位數字格式化數字。如果呼叫toFixed()不帶引數的方法,則它將返回一個四捨五入的整數。7、 toPrecision()

toPrecision()返回具有特定長度的數值。它接受一個表示長度的引數。如果給出的長度沒有特定長度,則該方法將按原樣返回數字。

// Example 1var num = 456.789;console.log(num.toPrecision())      // Output : 456.789// Example 2var num = 456.789;console.log(num.toPrecision(2))     // Output : 4.6
8、valueOf()

該valueOf() 方法用於返回Number你正在呼叫它的物件的原始值。JavaScript中的原始型別是數字,字串,bigint,符號,未定義,null和布林值。

let x = 45let num = x.valueOf()console.log(num)                 // Output: 45console.log(typeof num);         // Output: Number
9、toLocaleString()

該toLocaleString() 方法使用本地語言格式轉換數字並將其作為字串返回。它帶有兩個引數locales和options,它們定義了要使用的轉換所用的語言以及該函式的行為。

obj.toLocaleString([locales[, options]])
let num = 226537.883;//US Englishconsole.log(num.toLocaleString('en-US'));     //Output: 226,537.883// Romanian (Romania)console.log(num.toLocaleString('ro-RO'));     //Output: 226.537,883// Standard French (especially in France)console.log(num.toLocaleString('fr-FR'));     //Output: 226 537,883
10、 isInteger()

isInteger() 檢查給定值是否為整數,並返回布林值。

//Example 1let x = 10let num = Number.isInteger(x)console.log(num)                     // Output: true//Example 2let x = 10.99let num = Number.isInteger(x)console.log(num)                     // Output: false//Example 3let x = "10"let num = Number.isInteger(x)console.log(num)                     // Output: false
在上面的示例中,設定x為10返回布林值true;當我們將其更改為10.99它的值時,它將返回false;因為該值具有十進位制數字,這意味著它不是整數。同樣,當我們用引號將值引起來時,該方法返回false;因為該值是一個字串。11、 isFinite()

isFinite() 檢查給定值是否為有限值,並返回布林值。

//Example 1let x = 10let num = Number.isFinite(x)console.log(num)                     // Output: true//Example 2let x = -10.99let num = Number.isFinite(x)console.log(num)                     // Output: true//Example 3let x = "10"let num = Number.isFinite(x)console.log(num)                     // Output: false
將x設定為10與 -10.99時,inFinite()方法返回true,因為它們是有限數。 當我們將值更改為字串時,則返回false。12、 isSafeInteger()

isSafeInteger()檢查給定值是否為安全整數,並返回布林值。當所有整數都在(2 ^ 53–1)到-(2 ^ 53–1)之間時,該整數被視為安全整數。

Number.isSafeInteger(220)//輸出:true Number.isSafeInteger(-220)//輸出:true Number.isSafeInteger(2.2)//輸出:false Number.isSafeInteger(978678367894123469469410320213)//輸出:false
以上這些就是我要跟大家分享的12種JavaScript數字格式,可以幫助你操縱數字值並提高開發效率。

12
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 全面入門的Python逐步指南