亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在JavaScript中將String轉換為Number的最快方法是什么?

在JavaScript中將String轉換為Number的最快方法是什么?

慕斯709654 2019-11-27 10:26:10
任何數字,就是數字。字符串看起來像一個數字,它是數字。其他所有內容,都歸為NaN。'a' => NaN'1' => 11 => 1
查看完整描述

3 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

這是簡單的方法: var num = Number(str); 在此示例中,str是包含字符串的變量。您可以對其進行測試并查看其工作方式:Google chrome開發人員工具,然后轉到控制臺并粘貼以下代碼。閱讀評論以更好地了解轉換是如何完成的。


// Here Im creating my variable as a string

var str = "258";



// here im printing the string variable: str

console.log ( str );



// here Im using typeof , this tells me that the variable str is the type: string

console.log ("The variable str is type: " + typeof str);



// here is where the conversion happens

// Number will take the string in the parentesis and transform it to a variable num as type: number

var num = Number(str);

console.log ("The variable num is type: " + typeof num);


查看完整回答
反對 回復 2019-11-27
  • 3 回答
  • 0 關注
  • 715 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號