Home > Web Front-end > JS Tutorial > body text

How to use number object in javascript

青灯夜游
Release: 2018-12-27 13:15:10
Original
3155 people have browsed it

In JavaScript, the number object is very useful for dealing with numeric types. This article will introduce to you the usage of number object, I hope it will be helpful to you.

How to use number object in javascript

What is a number object?

In JavaScript, the number object is also called a number object, which is an object used to handle numeric types.

The number object has various functions for formatting numeric values, and can release various properties and methods for expressing values ​​such as maximum value, minimum value, infinity or infinity of numeric types.

How to use number object?

The following is an example of using the number object method to represent a numerical value.

1. Create a number object and check the data type

var num1 = new Number(123);
console.log(typeof(num1));
Copy after login

Rendering:

How to use number object in javascript

2. Convert numbers to strings

var num1 = 1337;
var num2=num1.toString();
console.log(num2);
console.log(typeof(num2));
Copy after login

Rendering:

How to use number object in javascript

3. Get up to 5 decimal places

var num1 = new Number(123.456789);
console.log(num1.toFixed(5));
Copy after login

Rendering:

How to use number object in javascript

4. Get up to 5 digits, including the integer part

var num1 =new Number(123.456789);
console.log(num1.toPrecision(5)); // 123.46
Copy after login

How to use number object in javascript

What is NaN?

NaN is a special value used to represent results that cannot be expressed numerically. For example, when performing an illegal operation such as "0 divided by 0", NaN is returned.

NaN value can be used to indicate that a certain value is not a number, for example:

<script type="text/javascript">
var Month=30;
if (Month < 1 || Month > 12){
Month = Number.NaN;
}
console.log(Month);
</script>
Copy after login

Rendering:

How to use number object in javascript

Summary: The above is this The entire content of this article, I hope it will be helpful to everyone's study.

The above is the detailed content of How to use number object in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!