Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand JavaScript numerical method

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to understand the JavaScript numerical method, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

What useful actions you can do with numbers using built-in methods and properties. Raw values, such as (25 or 3.14), cannot have properties or methods (because they are not objects).

But in JavaScript, methods and properties can also be used for raw values, because JavaScript treats the original values as objects when executing methods and properties. Three JavaScript numerical methods are described below.

1. ToExponential () method

The toExponential () method returns a numeric string.

This method takes an optional parameter that defines the number of characters after the decimal point.

Example:

Var num = 12.5; num.toExponential (); / / return 1.25e+1 num.toExponential (2); / / return 1.25e+1 num.toExponential (4); / / return 1.2500e+1 num.toExponential (6); / / return 1.250000e+1

Complete code:

JavaScript toExponential () method example-basic course (nhooo.com) JavaScript Number method

ToExponential () returns a string that represents a number as an exponential symbol:

The optional parameter defines the number of digits after the decimal point.

Var num = 12.5; document.getElementById ("para") [xss_clean] = num.toExponential () + "

"+ num.toExponential (2) +"

"+ num.toExponential (4) +"

"+ num.toExponential (6)

Note:

Exponential symbols can be used to represent numbers of a very large or very small order of magnitude. For example, 95700000000 can be written as 957e8 or 957e + 8.

2. ToFixed () method

The toFixed () method uses fixed-point notation to format numbers.

The value returned by this method is a string with the exact number of digits specified after the decimal point.

Example:

Var num = 12.525; num.toFixed (); / / return 13 num.toFixed (2); / / return 12.53 num.toFixed (4); / / return 12.5250 num.toFixed (6); / / return 12.525000

Running effect

If necessary, the number is rounded, and if necessary, the decimal part is filled with zero to have the specified length.

3. ToPrecision () method

The toPrecision () method returns a string that represents the number of the specified precision.

The value returned by this method is a string with the exact number of digits specified after the decimal point.

Example:

Var num = 5.123456; num.toPrecision (); / / 5.123456 num.toPrecision (1); / / 5 num.toPrecision (2); / / 5.1 num.toPrecision (3); / / 5.12 num.toPrecision (4); / / 5.123 num.toPrecision (10); / / 5.123456000

On how to understand the JavaScript numerical method to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report