How to make it smaller in javascript

WBOY
Release: 2023-05-15 22:45:08
Original
541 people have browsed it

In JavaScript, there are many methods to change the size of numbers. These methods are introduced one by one below:

  1. Math.floor()
    Math.floor() function returns less than or equal to The largest integer for a given number.

For example:

Math.floor(1.7); // Returns 1
Math.floor(-1.7); // Returns -2

  1. Math.ceil()
    Math.ceil() function returns the smallest integer greater than or equal to a given number.

For example:

Math.ceil(1.7); // Return 2
Math.ceil(-1.7); // Return -1

  1. Math.round()
    Math.round() function returns a number rounded to the nearest integer.

For example:

Math.round(1.7); // Returns 2
Math.round(-1.7); // Returns -2

  1. toFixed()
    toFixed() method can preserve the number of decimal places in a number.

For example:

var num = 7.123456;
console.log(num.toFixed(2)); // Output 7.12

  1. parseFloat()
    parseFloat() method can extract a floating point number from a string.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template