脚本宝典收集整理的这篇文章主要介绍了js实例Math(算数)对象,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。
源代码
<!DOCTYPE htML> <html> <body> <p id="demo">Click the button to round the number 2.5 to ITs nearest integer.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { document.getElementById("demo").innerHTML=Math.round(2.5); } </script> </body> </html>
运行结果
使用 random() 来返回 0 到 1 之间的随机数
源代码
<!DOCTYPE html> <html> <body> <p id="demo">Click the button to display a random number.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { document.getElementById("demo").innerHTML=Math.random(); } </script> </body> </html>
运行结果
使用 max() 来返回两个给定的数中的较大的数
源代码
<!DOCTYPE html> <html> <body> <p id="demo">Click the button to return the highest number of 5 and 10.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { document.getElementById("demo").innerHTML=Math.max(5,10); } </script> </body> </html>
运行结果
使用 min() 来返回两个给定的数中的较小的数
源代码
<!DOCTYPE html> <html> <body> <p id="demo">Click the button to return the lowest number of 5 and 10.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { document.getElementById("demo").innerHTML=Math.min(5,10); } </script> </body> </html>
运行结果
摄氏度与华氏转换
源代码
<!DOCTYPE html> <html> <head> <script> function convert(degree) { if (degree=="C") { F=document.getElementById("c").value * 9 / 5 + 32; document.getElementById("f").value=Math.round(F); } else { C=(document.getElementById("f").value -32) * 5 / 9; document.getElementById("c").value=Math.round(C); } } </script> </head> <body> <p></p><b>Insert a number into one of the input fields below:</b></p> <form> <input id="c" name="c" onkeyup="convert('C')"> degrees Celsius<br> equals<br> <input id="f" name="f" onkeyup="convert('F')"> degrees Fahrenheit </form> <p>Note that the <b>Math.round()</b> method is used, so that the result will be returned as an integer.</p> </body> </html>
运行结果
源代码
<!DOCTYPE html> <html> <body> <p id="demo">Click the button to round the number 2.5 to its nearest integer.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { document.getElementById("demo").innerHTML=Math.round(2.5); } </script> </body> </html>
运行结果
使用 random() 来返回 0 到 1 之间的随机数
源代码
<!DOCTYPE html> <html> <body> <p id="demo">Click the button to display a random number.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { document.getElementById("demo").innerHTML=Math.random(); } </script> </body> </html>
运行结果
使用 max() 来返回两个给定的数中的较大的数
源代码
<!DOCTYPE html> <html> <body> <p id="demo">Click the button to return the highest number of 5 and 10.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { document.getElementById("demo").innerHTML=Math.max(5,10); } </script> </body> </html>
运行结果
使用 min() 来返回两个给定的数中的较小的数
源代码
<!DOCTYPE html> <html> <body> <p id="demo">Click the button to return the lowest number of 5 and 10.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { document.getElementById("demo").innerHTML=Math.min(5,10); } </script> </body> </html>
运行结果
摄氏度与华氏转换
源代码
<!DOCTYPE html> <html> <head> <script> function convert(degree) { if (degree=="C") { F=document.getElementById("c").value * 9 / 5 + 32; document.getElementById("f").value=Math.round(F); } else { C=(document.getElementById("f").value -32) * 5 / 9; document.getElementById("c").value=Math.round(C); } } </script> </head> <body> <p></p><b>Insert a number into one of the input fields below:</b></p> <form> <input id="c" name="c" onkeyup="convert('C')"> degrees Celsius<br> equals<br> <input id="f" name="f" onkeyup="convert('F')"> degrees Fahrenheit </form> <p>Note that the <b>Math.round()</b> method is used, so that the result will be returned as an integer.</p> </body> </html>
运行结果
觉得可用,就经常来吧!Javascript技巧 脚本宝典 欢迎评论哦! js技巧,巧夺天工,精雕玉琢。小宝典献丑了!
以上是脚本宝典为你收集整理的js实例Math(算数)对象全部内容,希望文章能够帮你解决js实例Math(算数)对象所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。