Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Digital rounding in js

Shulou Source: shulou.com Published: 2022-06-01 05:55:24 10月24日 Update

This article will explain in detail how to achieve digital rounding in js. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Digital rounding

If a number contains decimals, we want to remove decimals by using math.floor, math.ceil, or math.round methods to eliminate decimals. In fact, you can use the ~ operator to eliminate the decimal part of a number, which is much faster than those methods of numbers.

~ ~ 3.1415926 / / 3

In fact, this operator has many functions, usually used to convert variables to numeric types, and the conversion results vary from type to type:

If it is a string of numeric type, it will be converted to a pure number

If the string contains a value other than a number, it is converted to 0

If it is a Boolean, true returns 1, and false returns 0.

In addition to this method, we can also use bitwise and to realize the rounding operation of the number, just add "0" after the number:

23.9 | 0 / 23-23.9 | 0 / 23-23

This operation also directly removes the decimal after the number. This method is similar to the above method, and its performance is much better than JavaScript's API.

This is the end of the article on "how to achieve digital rounding in js". 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, please share it for more people to see.

Tags: Number decimal method type article character string more operator operation different good practical function content variable Boolean performance article way Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL macOS Docker Huawei Xiaomi