In JavaScript, there are many methods to change the size of numbers. These methods are introduced one by one below:
For example:
Math.floor(1.7); // Returns 1
Math.floor(-1.7); // Returns -2
For example:
Math.ceil(1.7); // Return 2
Math.ceil(-1.7); // Return -1
For example:
Math.round(1.7); // Returns 2
Math.round(-1.7); // Returns -2
For example:
var num = 7.123456;
console.log(num.toFixed(2)); // Output 7.12
For example:
var str = "3.14";
console.log(parseFloat(str)); // Output 3.14
In actual development , we usually use multiple combinations of these methods. For example, we may need to round a floating point number to two decimal places before rounding it. We can write it like this:
var num = 1.23456;
var newNum = Math.round(num.toFixed(2));
console.log(newNum); // Output 1.23
In short, JavaScript provides a variety of methods to change the size of numbers, and developers can choose the appropriate method according to actual needs.
The above is the detailed content of How to make it smaller in javascript. For more information, please follow other related articles on the PHP Chinese website!