[JavaScript-String類]字串轉換數字

程式範例:

 var a = "10";//整數 var b = "12.5";//浮點數 //字串數字相加 ,但加出來不是你想要的。 document.write("a+b:"+(a+b)); document.write("<br>"); //必須轉型才能相加 var a_Int = parseInt(a); var b_Float = parseFloat(b); //字串數字相加 document.write("a+b:"+(a_Int+b_Float)); document.write("<br>"); //浮點數 用整數轉換 var b_Int = parseInt(b); document.write("b:"+(b_Int));


說明:

parseInt();     將字串轉換為字串
      parseFloat(); 將字串轉換為有小數(浮點數)

測試結果:

 
 










 















其它文章

arrow
arrow

    PG Levin Li 發表在 痞客邦 留言(0) 人氣()