Often the result of calculations is a number with a large number of decimal places. If this number is used for further calculations, then it can be left in this form. But sometimes you need to round the number, for example, to display on the page. There are several methods for rounding numbers in JavaScript.
The Math.round() method rounds the value to an integer.
Math round (number)
The number is rounded according to mathematical rules. That is, if after the decimal point there is a number from 0 to 4, then the fractional part is simply discarded. And if after the decimal point there is a number from 5 to 9, then the fractional part is discarded, and one is added to the whole part. example:
JavaScript:
There are two more methods that round a number to an integer value. The Math.floor() method rounds down. It discards the fractional part of the number. And the Math.ceil() method rounds up. It discards the fractional part, and adds one to the integer part. Example:
Of course, 5 - (-2) is 5+2. Do not forget that you will not get the number 5 in this formula. The maximum will be 4.999999999. The resulting values can be rounded to the desired accuracy.
If only whole numbers are needed, then the resulting values can be rounded down to the nearest whole number. One must be added to the maximum so that this maximum is also possible. The formula looks like this:
integer = Math.floor (min + Math.random() * (max + 1 - min)
Let's output numbers from 10 to 15:
20 |
for (i=1; i= (greater than or equal to), 3); //false console.log(5>=3); //true When comparing numbers with a fractional part, it is necessary to take into account the errors that may occur during these calculations. For example, in JavaScript, the sum of the numbers (0.2 + 0.4) does not equal 0.6: Console.log((0.2+0.4)==0.6); //false Errors occur because all calculations are computer or other electronic device produces in 2 number system. Those. before performing any actions, the computer must first convert the numbers presented in the expression to the 2 number system. But, not any fractional decimal number can be represented exactly in the 2nd number system. For example, the number 0.25 10 is converted to binary exactly. 0.125 × 2 = 0.25 | 0 0.25 × 2 = 0.5 | 0 0.5 × 2 = 1 | 1 0.125 10 = 0.001 2 For example, the number 0.2 10 can be converted to the 2 system only with a certain accuracy: 0.2 × 2 = 0.4 | 0 0.4 × 2 = 0.8 | 0 0.8 × 2 = 1.6 | 1 0.6 × 2 = 1.2 | 1 0.2 × 2 = 0.4 | 0 0.4 × 2 = 0.8 | 0 0.8 × 2 = 1.6 | 1 0.6 × 2 = 1.2 | 1 0.2 × 2 = 0.4 | 0 0.4 × 2 = 0.8 | 0 0.8 × 2 = 1.6 | 1 0.6 × 2 = 1.2 | 1 ... 0.2 10 = 0.001100110011... 2 As a result, these errors will affect the calculation of the sum of two numbers and the comparison results. Those. it turns out that in fact JavaScript will see this entry as follows: 0.6000000000000001==0.6 When calculating or displaying numbers with a fractional part, you must always specify the precision with which this is to be done. For example, compare numbers up to 2 decimal places using the toFixed() and toPrecision() methods: //method toFixed() console.log((0.2+0.4).toFixed(2)==(0.6).toFixed(2)); //true //toPrecision() method console.log((0.2+0.4).toPrecision(2)==(0.6).toPrecision(2)); //true Basic math operationsJavaScript has the following mathematical operators: + (addition), - (subtraction), * (multiply), / (division), % (modulo), ++ (increase value by 1), -- (decrease value by 1). 6+3 //9 6-3 //3 6*3 //18 6/3 //2 6%3 //0, i.e. 6:3=2 => 6-3*2 => rest(0) 5%2 //1, i.e. 5:2=2(.5) => 5-2*2 => rest(1) 7.3%2 //1.3, i.e. 7.3:2=3(.65) => 7.3-2*3 => rest(1.3) //the sign of the operation result % is equal to the sign of the first value -9%2.5 //-1.5, i.e. 9:2.5=3(.6) => 9-2.5*3 => rest(1.5) -9%-2.5 //-1.5, i.e. 9:2.5=3(.6) => 9-2.5*3 => rest(1.5) -2%5 //-2, i.e. 2:5=0(.4) => 2-5*0 => rest(2) x = 3; console log(x++); //displays 3, then sets y to 4 console.log(x); //4 x = 3; console log(++x); //sets 4 and outputs x = 5; console log(x--); //outputs 5, y then sets 4 console.log(x); //4 x = 5; console log(--x); //sets 4 and outputs Besides, there are combined operators in JavaScript: x+=y (x=x+y), x-=y (x=x-y), x*=y (x=x*y), x/=y (x=x/y), x%=y (x=x%y). x=3; y=6; x+=y; console log(x); //9 x = 3; y=6; x-=y; console log(x); //-3 x = 3; y=6; x*=y; console log(x); //18 x = 3; y=6; x/=y; console log(x); //0.5 x = 3; y=6; x%=y; console log(x); //3 Now consider the floor method (in translation - gender), which works the opposite of the ceil method, i.e. it rounds the fractional number DOWN.
var age = 35.97 ; age = Math.floor(age) ; /* Round the value of age variable down */ document.write(age);
As you can see, the floor method rounded the number 35.97 down to 35 . Even though 0.97 is greater than 0.5 (cm. ). This lesson looked at the methods of the Math object that allow you to round fractional decimal numbers. Now you need to do your homework. Your task is to write a function that takes two parameters. At the output, the function should output the same array, but at the same time, all elements of the array should be rounded using the method of the Math object specified in the second parameter. Source array: varnumberArray = ; At first, the solution to this task may seem almost identical to the solutions to the homework problems from the first three lessons. this topic. But not everything is so simple... Decision #1 - Attention According to the condition of the problem, the function must take two parameters - the original array and one of the methods: "round" , "ceil" or "floor" . Based on this, I tried to do this...
function decimal (anyArray ,method ) /* Create a function with two parameters */ for (i = 0 ; i< anyArray .length ; i ++ ) { anyArray = Math.method(anyArray); /* Use one of the methods of the Math object to round the current element of the array */ document.write(anyArray + " "
) decimal (numberArray, round ) /* Call the function and give it two parameters. But DO NOT specify a method NAME as a function parameter */
In this solution, we create a function with two parameters, and when we call it, we try to specify the original array and the NAME of one method as parameters of the function: But we will not get the result, since it is IMPOSSIBLE to specify the NAME of the method as a function parameter. Please note: it is not by chance that the names of the methods "round" , "ceil" and "floor" are enclosed in quotes in the problem statement. decimal (numberArray, "round" ) - but such a notation will not be correct either!!! Solution #2 - Correcting the previous solution You can solve the problem by specifying one parameter for the function.
varnumberArray = ; function decimal (anyArray ) /* Create a function with one parameter */ for (i = 0 ; i< anyArray .length ; i ++ ) /* Перебираем элементы массива */ { /* Use the Math object's round method to round the current element of the array */ document.write (anyArray + " - Rounded element " ) /* Display the ROUNDED element of the array */ decimal (numberArray ) /* Call the function and specify one parameter - the original array */
We managed to achieve the desired result here: the round method rounded all numbers by . But the condition is not met, because the function takes only one parameter. Solution #3 - Function with two parameters Here the problem is solved correctly. For this, it was necessary to remember subject conditions in javascript and apply multiple conditions simultaneously.
varnumberArray = ; function decimal(anyArray ,method ) if (method "
) Else if(method "
) else if(method "
) }
34.82 - original array element 12.9 - original array element 17.01 - the original element of the array 78.51 - original array element This is the correct solution for homework. Here, two parameters are specified for the function according to the condition. Try in the last line of this solution: Solution #4 - Function with two parameters + prompt method I decided to optimize the previous solution a bit and added prompt method, which calls modal window A that contains a field for entering information. Now, thanks to this, it will be possible to enter the name of one of the round , floor or ceil methods in the input field and get the corresponding result.
varnumberArray = ; function decimal(anyArray ,method ) if (method == "round" ) /* 1st condition */ "
) Else if(method == "ceil" ) /* 2nd condition */ "
) else if(method == "floor" ) /* 3rd condition */ "
) /* Add prompt method */ var method = prompt("Enter one of the methods: round, ceil or floor" ); if (method == "floor" ) /* 1st condition */ "
) else if (method == "round" ) /* 2nd condition */ "
) else if (method == "ceil" ) /* 3rd condition */ "
) else /* Else... */ "
) decimal (numberArray, method ) /* Call the function */
This is how the round , floor , or ceil methods of the Math object work, which round fractional numbers. Thematic materials:
Updated: 04/20/2021
103583
If you notice an error, select a piece of text and press Ctrl + Enter
Site categories |