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 integer javascript without decimals

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

Share

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

This article is to share with you about how javascript does not have decimals. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Methods: 1, rounding with "parseInt" sentence, 2, rounding with "numerical .tofixed (0)" sentence, 3, rounding with "Math.ceil" sentence, 4, rounding with "Math.floor" sentence, 5, rounding with "Math.round" sentence, etc.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

In javascript, a number can be rounded if it does not want the decimal part (decimal part removed). Here are some methods of javascript rounding.

The method of how to round up javascript (not decimal)

1. ParseInt ()

/ / js built-in function, note that the accepted parameter is string, so there is a type conversion parseInt (1.5555) / / = > 1 when the method is called

2. Number.toFixed (0)

/ / Note the string returned by toFixed. If you want to get an integer, you need to do type conversion 1.5555.toFixed (0) / / = > "1".

3. Math.ceil ()

/ / rounding up Math.ceil (1.5555) / / = > 2

4. Math.floor ()

/ / rounding down Math.floor (1.5555) / / = > 1

5. Math.round ()

/ / rounding Math.round (1.5555) / / = > 2Math.round (1.4999) / / = > 1

6. Math.trunc ()

/ / discarding decimal rounding Math.trunc (1.5555) / / = > 1

7. Double bit non-rounding

/ / rounding using bit operation. Only 32-bit signed integers are supported. Decimal places will be discarded. Same as ~ 1.5555 / / > 1 below.

8. To transport or round up by bit.

1.5555 | 0 / / = > 1

9. Bitwise XOR rounding

1.5555 ^ 0 / / = > 1 Thank you for reading! This is the end of the article on "how to do not decimal javascript". I hope the above content can be of some help to you, so that 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