[JavaScript] JavaScript learning 27th JavaScript math (arithmetic) object

Source: Internet
Author: User
Tags natural logarithm

Math (arithmetic) objects are used to execute common arithmetic tasks.

Instance
Round ()
How to use round ().
Random ()
How to use random () to return a random number between 0 and 1.
Max ()
How to Use max () to return a large number of two given numbers.
Min ()
How to Use min () to return a small number of two given numbers.
Complete math Object Reference Manual

We provide reference manuals for JavaScript math objects, including all the attributes and methods that can be used for array objects.

This Manual contains a detailed description of each attribute and method and related examples.

Math object

Math (arithmetic) objects are used to execute common arithmetic tasks.

The math object provides multiple numeric types and functions. You do not need to define this object before using it.

Calculate the value

Javascript provides eight computing values that can be accessed by math objects:

  • Constant
  • Circumference Rate
  • Square root of 2
  • Square root of 1/2
  • Natural logarithm of 2
  • Natural logarithm of 10
  • Base-2 logarithm of E
  • Base-10 logarithm of E

This is a method to use these values in javascript: (one-to-one correspondence with the preceding calculated values)

  • Math. e
  • Math. Pi
  • Math. sqrt2
  • Math. sqrt1_2
  • Math. ln2
  • Math. ln10
  • Math. log2e
  • Math. log10e
Arithmetic Method

In addition to the value that can be accessed by the math object, there are several functions (methods) that can be used.

Function (method) instance:

The following example uses the round method of the math object to round a number.

document.write(Math.round(4.7))

The above code output is:

5

The following example uses the random () method of the math object to return a random number between 0 and 1:

document.write(Math.random())

The above code output is:

0.9370844220218102

The following example uses the floor () method and random () of the math object to return a random number between 0 and 10:

document.write(Math.floor(Math.random()*11)) 

The above code output is:

3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.