JavaScript中類型的一些方法

來源:互聯網
上載者:User

Number類型的幾個方法

1,toFixed()

返回具有指定位元小數的數字

var oValue = new Number(99);
alert(oValue.toFixed(2))    //output 99.00

2,toExponential()

返回用科學計數法表示的數位字串形式

var oValue = new Number(99);
alert(oValue.toExponential(1)); //output 9.9e+1

3,toPrecision()

根據最有意義的形式返回數位預定形式或指數形式

var oValue = new Number(99);
alert(oValue.toPrecision(1));  //output 1e+2
alert(oValue.toPrecision(2));  //output 99
alert(oValue.toPrecision(3));  //output 99.0

 

以上三個方法都會進行舍入操作。

字串類型的幾個方法:

1,charAt和charCodeAt

訪問字串中的單個字元或字元代碼

var oValue = new String("hello");
alert(oValue.chatAt(1));          //output e
alert(oValue.chatCodeAt(1));      //output 101

 

2,silce和substring

var oValue = new String("hello world");
alert(oValue.slice(-3));                  //output rld
alert(oValue.substring(-3));              //output hello world
alert(oValue.slice(3,-4));                //output lo w
alert(oValue.substring(3,-4));            //output hel

 

Globald對象

URI方法encodeURI、encodeURIComponent、decodeURI和decodeURIComponent代替了BOM的escape()和unescape()方法。URI方法更可取,因為它們會對所有Unicode符號變嗎,而BOM方法只能對ASCII符號正確編碼。盡量避免使用escape()和unescape()方法。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.