Math floor javascript что это
Перейти к содержимому

Math floor javascript что это

  • автор:

JavaScript Math Object

The JavaScript Math object allows you to perform mathematical tasks on numbers.

Example

The Math Object

Unlike other objects, the Math object has no constructor.

The Math object is static.

All methods and properties can be used without creating a Math object first.

Math Properties (Constants)

The syntax for any Math property is : Math.property .

JavaScript provides 8 mathematical constants that can be accessed as Math properties:

Example

Math Methods

The syntax for Math any methods is : Math.method(number)

Number to Integer

There are 4 common methods to round a number to an integer:

Math.round(x) Returns x rounded to its nearest integer
Math.ceil(x) Returns x rounded up to its nearest integer
Math.floor(x) Returns x rounded down to its nearest integer
Math.trunc(x) Returns the integer part of x (new in ES6)

Math.round()

Math.round(x) returns the nearest integer:

Examples

Math.ceil()

Math.ceil(x) returns the value of x rounded up to its nearest integer:

Example

Math.floor()

Math.floor(x) returns the value of x rounded down to its nearest integer:

Example

Math.trunc()

Math.trunc(x) returns the integer part of x:

Example

Math.sign()

Math.sign(x) returns if x is negative, null or positive:

Example

Math.trunc() and Math.sign() were added to JavaScript 2015 — ES6.

Math.pow()

Math.pow(x, y) returns the value of x to the power of y:

Example

Math.sqrt()

Math.sqrt(x) returns the square root of x:

Example

Math.abs()

Math.abs(x) returns the absolute (positive) value of x:

Example

Math.sin()

Math.sin(x) returns the sine (a value between -1 and 1) of the angle x (given in radians).

If you want to use degrees instead of radians, you have to convert degrees to radians:

Angle in radians = Angle in degrees x PI / 180.

Example

Math.cos()

Math.cos(x) returns the cosine (a value between -1 and 1) of the angle x (given in radians).

If you want to use degrees instead of radians, you have to convert degrees to radians:

Angle in radians = Angle in degrees x PI / 180.

Example

Math.min() and Math.max()

Math.min() and Math.max() can be used to find the lowest or highest value in a list of arguments:

Example

Example

Math.random()

Math.random() returns a random number between 0 (inclusive), and 1 (exclusive):

Example

You will learn more about Math.random() in the next chapter of this tutorial.

The Math.log() Method

Math.log(x) returns the natural logarithm of x.

The natural logarithm returns the time needed to reach a certain level of growth:

Examples

Math.E and Math.log() are twins.

How many times must we multiply Math.E to get 10?

The Math.log2() Method

Math.log2(x) returns the base 2 logarithm of x.

How many times must we multiply 2 to get 8?

The Math.log10() Method

Math.log10(x) returns the base 10 logarithm of x.

How many times must we multiply 10 to get 1000?

JavaScript Math Methods

Method Description
abs(x) Returns the absolute value of x
acos(x) Returns the arccosine of x, in radians
acosh(x) Returns the hyperbolic arccosine of x
asin(x) Returns the arcsine of x, in radians
asinh(x) Returns the hyperbolic arcsine of x
atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
atan2(y, x) Returns the arctangent of the quotient of its arguments
atanh(x) Returns the hyperbolic arctangent of x
cbrt(x) Returns the cubic root of x
ceil(x) Returns x, rounded upwards to the nearest integer
cos(x) Returns the cosine of x (x is in radians)
cosh(x) Returns the hyperbolic cosine of x
exp(x) Returns the value of E x
floor(x) Returns x, rounded downwards to the nearest integer
log(x) Returns the natural logarithm (base E) of x
max(x, y, z, . n) Returns the number with the highest value
min(x, y, z, . n) Returns the number with the lowest value
pow(x, y) Returns the value of x to the power of y
random() Returns a random number between 0 and 1
round(x) Rounds x to the nearest integer
sign(x) Returns if x is negative, null or positive (-1, 0, 1)
sin(x) Returns the sine of x (x is in radians)
sinh(x) Returns the hyperbolic sine of x
sqrt(x) Returns the square root of x
tan(x) Returns the tangent of an angle
tanh(x) Returns the hyperbolic tangent of a number
trunc(x) Returns the integer part of a number (x)

Complete Math Reference

For a complete reference, go to our Complete Math Object Reference.

The reference contains descriptions and examples of all Math properties and methods.

JavaScript Math Functions Explained

JavaScript Math Functions Explained

Math is one of JavaScript’s global or standard built-in objects, and can be used anywhere you can use JavaScript. It contains useful constants like π and Euler’s constant and functions such as floor() , round() , and ceil() .

In this article, we’ll look at examples of many of those functions. But first, let’s learn more about the Math object.

Example

The following example shows how to use the Math object to write a function that calculates the area of a circle:

Math Max

Math.max() is a function that returns the largest value from a list of numeric values passed as parameters. If a non-numeric value is passed as a parameter, Math.max() will return NaN .

An array of numeric values can be passed as a single parameter to Math.max() using either spread (. ) or apply . Either of these methods can, however, fail when the amount of array values gets too high.

Syntax

Parameters

Numbers, or limited array of numbers.

Return Value

The greatest of given numeric values, or NaN if any given value is non-numeric.

Examples

Numbers As Parameters

Array As Parameter, Using Spread(…)

Array As Parameter, Using Apply

Math Min

The Math.min() function returns the smallest of zero or more numbers.

You can pass it any number of arguments.

Math PI

Math.PI is a static property of the Math object and is defined as the ratio of a circle’s circumference to its diameter. Pi is approximately 3.14149, and is often represented by the Greek letter π.

Examples

More Information:

Math Pow

Math.pow() returns the value of a number to the power of another number.

Syntax

Math.pow(base, exponent) , where base is the base number and exponent is the number by which to raise the base .

pow() is a static method of Math , therefore it is always called as Math.pow() rather than as a method on another object.

Examples

Math Sqrt

The function Math.sqrt() returns the square root of a number.

If a negative number is entered, NaN is returned.

sqrt() is a static method of Math , therefore it is always called as Math.sqrt() rather than as a method on another object.

Syntax

Math.sqrt(x) , where x is a number.

Examples

Math Trunc

Math.trunc() is a method of the Math standard object that returns only the integer part of a given number by simply removing fractional units. This results in an overall rounding towards zero. Any input that is not a number will result in an output of NaN.

Careful: This method is an ECMAScript 2015 (ES6) feature and thus is not supported by older browsers.

Examples

Math Ceil

The Math.ceil() is a method of the Math standard object that rounds a given number upwards to the next integer. Take note that for negative numbers this means that the number will get rounded “towards 0” instead of the number of greater absolute value (see examples).

Examples

Math Floor

Math.floor() is a method of the Math standard object that rounds a given number downwards to the next integer. Take note that for negative numbers this means that the number will get rounded “away from 0” instead of to the number of smaller absolute value since Math.floor() returns the largest integer less than or equal to the given number.

Examples

An application of math.floor: How to Create a JavaScript Slot Machine

For this exercise, we have to generate three random numbers using a specific formula and not the general one. Math.floor(Math.random() * (3 — 1 + 1)) + 1;

Another example: Finding the remainder

Example

Usage

In mathematics, a number can be checked even or odd by checking the remainder of the division of the number by 2.

Note Do not confuse it with modulus % does not work well with negative numbers.

JavaScript: Math floor() function

This JavaScript tutorial explains how to use the math function called floor() with syntax and examples.

Description

In JavaScript, floor() is a function that is used to return the largest integer value that is less than or equal to a number. In other words, the floor() function rounds a number down and returns an integer value. Because the floor() function is a static function of the Math object, it must be invoked through the placeholder object called Math.

Syntax

In JavaScript, the syntax for the floor() function is:

Parameters or Arguments

Returns

The floor() function returns the largest integer value that is less than or equal to a number.

  • Math is a placeholder object that contains mathematical functions and constants of which floor() is one of these functions.

Example

Let’s take a look at an example of how to use the floor() function in JavaScript.

In this example, we have invoked the floor() function using the Math class.

We have written the output of the floor() function to the web browser console log, for demonstration purposes, to show what the floor() function returns.

The following will be output to the web browser console log:

In this example, the first output to the console log returned 32 which is 32.65 rounded down to an integer value.

The second output to the console log returned 8 which is 8.1 rounded down to an integer value.

The third output to the console log returned -5 which is -4.2 rounded down to an integer value. Notice that when dealing with negative numbers, the floor() function rounds away from zero and in this case, returned -5 and not -4.

Methods: Math.floor()

JavaScript is a programming language full of flexible solutions for each problem when working with integers. Once you start working with arrays containing numbers, you might need to sort them out or return a particular number.

In this case, the Math.floor() is a function that returns the largest integer less than or equal to a specified number. What makes this function so practical is its variety of use cases when working with arrays and various data sets.

Syntax and basics of Math.floor()

When it comes to syntax, this function works quite efficiently and straightforwardly. This JavaScript function is depicted as a code in the following manner:

Please note that x is a number that represents the largest integer, which is less than or equal to the number you specify. Now, when you’re aware of how to write this function syntax-wise, let’s review the most basic use case of this method:

In this case, we indicate the number in the brackets of Math.floor() function, which is 7.99. We intend to get the number below or equal 7.99, which is 7. So, when you run this code through a compiler, the output you get is 7. While this logic seems quite okay, applying Math.floor() to larger data arrays proves useful for finding an integer in complex code applications.

The uniqueness of Math.floor()

One of the unique features of this function we review is its behavior when the number is not specified. Let’s try including null to the brackets and see what result it returns to use:

We use the same code structure, not the number, but null to see what output it can bring. If you work with this example on your own, the return you’ll get would be 0 , but not NaN . Why’s that? Not only floor() is a static method of math , but it also works with integers only, resulting in a 0 return in our example.

The good news is that you can also manipulate the Math.floor() function to work with multiple lines with numbers at the same time. Take a look:

In the first example, the returned integer cannot be less than 2, thus resulting in 2 as a return. But with cases of 3.6 or 7.5, it doesn’t matter what numbers go after a solid number. Accordingly, the outputs for the second and third examples would be 3 and 7 .

Another peculiar feature of Math.floor() function is how it works with negative numbers. It is no surprise that dozens of JavaScript commands work properly when the positive and negative numbers are specified. Let’s try using the example that capitalizes the negative number:

As you can see, we included the -87.9 , which is a negative number. The logic of math.floor() function for this example is to use rounds vice versa. It means that the function rounds away from zero, returning -88 and not -87 as you might have theoretically expected.

Various number types

Because this function has varied functionality, you might have thought it is a universal command you’d be using to find an integer. However, when you’re trying to work with money, Math.floor() proves to be an inferior way, compared to Math.round() , for instance. What differentiates these two approaches is how they deal with the nearest integers via rounding.

The most common example we should provide is the use of Math.round() for quite the same numbers we indicated before. Take a look:

When using math.round() , the output will heavily rely on numbers rounded to the nearest integer. Aren’t you wondering why that happened with the Math.round() function? Because it returns the value of a number rounded, the output is different from what you previously got with the Math.floor() function.

Including non-numeric types

Alright, we’ve already learned how the Math.floor() function works, what behavior it shows when working with negative numbers, and why the Math.round() method might work better in certain instances. What if we tell you that you can also experiment with this function by including the non-numeric types:

In this case, we include the string, which is learningjavascript , and the output you get using Math.floor() function would be different than 0. In particular, this function will result in an output equaling NaN. It happens because the object you’ve included in the brackets is not a non-numeric type.

When you try using the Math.round() function, the return would be just the same. Thus, don’t forget that you should capitalize on using only numeric types to get the desired integer output.

Test yourself

As a reminder, this function returns the largest integer less than or equal to a number you’ve specified. In contrast, Math.round() also returns an integer, rounded to the nearest number. Please note that if you include 0 in the brackets, the return you get equals 0, but not NaN.

Also, to acquire the lessons you’ve learned in this tutorial, we’ve prepared a few tasks you can do on your own, including:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *