JavaScript&jQuery.String對象.Number對象.Math對象

來源:互聯網
上載者:User

標籤:number   split   操作   lower   ast   分解   har   one   lan   

String對象.Number對象.Math對象

String對象

String 對象的屬性和方法用於操作字串。

<!DOCTYPE html>

<html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><body><script>var str="I like javascript ";document.write(str);document.write(‘<br>‘);// 屬性長度document.write(str.length);document.write(‘<br>‘);// 轉大寫document.write(str.toUpperCase());document.write(‘<br>‘);// 轉小寫document.write(str.toLowerCase());document.write(‘<br>‘);// 返回指定位置的字元,不包括空document.write(str.charAt(5));document.write(‘<br>‘);// 返回字元的位置document.write(str.indexOf(‘a‘));document.write(‘<br>‘);// 返回字元最後一次出現的位置document.write(str.lastIndexOf(‘a‘));document.write(‘<br>‘);// 從字串中取指定範圍的字元,從開始,包括空格document.write(str.substring(0,4));document.write(‘<br>‘);// 將字串按分解規則分解成數組var value=str.split(" ");document.write(value[0]);document.write(‘<br>‘);// 去年字串開始和結尾的空格document.write(str.trim());document.write(‘<br>‘);// 查看和替換document.write(str.replace(‘javascript‘,‘C++‘));document.write(‘<br>‘);</script></body></html>Math對象

Number對象通常用於運算元字。

<!DOCTYPE html>

<html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><body><div id="info"></div><script>var numberOne=12.545678;// 四捨五入document.write(numberOne.toFixed());document.write(‘<br>‘);// 指定有效數個數,這裡指定為3document.write(numberOne.toPrecision(3));document.write(‘<br>‘);//以指數標記法返回該數值字串表示形式,可以帶參數指定小數位元document.write(numberOne.toExponential(3));document.write(‘<br>‘);</script></body></html>Math對象Math對象用於數學上的計算,如算平方,四捨五入,產生隨機數等等。<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><body><script>    var numberTwo=23.344;    // 常量PI    document.write(Math.PI);    document.write(‘<br>‘);    // 產生隨機數,0~1,範圍從0到1以內,不包括1    document.write(Math.random());    document.write(‘<br>‘);    // 將整數,取最近並大於它當前值的數    document.write(Math.ceil(numberTwo));    document.write(‘<br>‘);    // 將整數,取最近並小於它當前值的數    document.write(Math.floor(numberTwo));    document.write(‘<br>‘);    // 四捨五入    document.write(Math.round(numberTwo));    document.write(‘<br>‘);</script></body></html> 

JavaScript&jQuery.String對象.Number對象.Math對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.