1. 程式人生 > >JS數字轉字串,字串轉數字

JS數字轉字串,字串轉數字

//--------------------字串轉數字---------------------------
    var s = "234";
    //1、純數字轉換


    //字串在運算操作中會被當做數字型別來處理
    s *= 1;


    //2 字元前加“+”
    console.log(+s);


    //string的兩個轉換函式,只對string有效
    parseInt(s); // 234
    parseFloat(s); //234


    //強制型別轉換
    Number(s); // 234






    //2、數字加字母等非數字轉換
    var s = '234string';
    parseInt(s); //234
    parseFloat(s);  //234.0








    //--------------------數字轉換字串---------------------------
    var num=123123;
    //1、toString()
    console.log(num.toString());


    //2、數字+任何字串""
    console.log(num+"");


    // 關於字串


    //判斷字串是否包含 包含返回下標 不包含返回-1
    var i = str.indexOf("x")


    //最後一次下標的字串的字串
    var i = str .lastIndexOf("")


    //轉化大小寫
    str.toUpperCase() //轉大寫
    str.tolowerCase()// 轉小寫


    //擷取字串 字串.substr("從哪開始","擷取幾個")
    str.substr(0,5)

//計算次方

alert(Math.pow(2,4));//計算2的4次方