Discover how to round decimals to 1 decimal place, include the articles, news, trends, analysis and practical advice about how to round decimals to 1 decimal place on alibabacloud.com
Adjust the decimal format, such as retaining the last two digits of the decimal point, the following articles collect some good implementation methods and share some of them. During the development process, we often encounter the need to adjust the
In the development process, you often encounter formatting to adjust decimals, such as two digits after the decimal point, and so on. Methods are also quite common, as is the memo below.
The first, using Math.Round
var original=28.453
1)//round
PHP rounding, rounding, round function Use example, rounding round
Decimal Example:
PHP reserved two decimal places and rounded
Copy the Code code as follows:$n = 0.1265489;Echo sprintf ("%.2f", $n); 0.13
As you can see, we used the sprintf
Example of PHP rounding, rounding, and using the round function. Example of using the PHP rounding, rounding, and round functions, and rounding the round decimal places: PHP keeps two decimal places and rounds them to copy the code as follows: $ n0.1
The integer function is often used. let's make a summary today! Actually, it's quite simple, just a few functions ~~ Major methods: ceil, floor, round, intval integer methods such as rounding, rounding, and ignoring decimals
There are four common
Decimal Example:
PHP reserved two decimal places and rounded
Copy the Code code as follows:$n = 0.1265489;Echo sprintf ("%.2f", $n); 0.13
As you can see, we used the sprintf function to format the $n%.2f is the target format, where 2 means that two-
Is the result of LUA calculation, you can see the result is very accurate.
Is the result of Python3 calculation, you can see that the calculation results are inaccurate:
The decimal calculation in JavaScript is not accurate, see this problem
Why are
See a lot of people have this reserved number of decimal point after the demand, but most of them write a function to intercept, but also consider rounding up what, it is quite complicated to write.
In fact, the number object of JavaScript has a
We can see that many people want to retain the decimal point after the number, but most of them write a function to intercept the number, and consider rounding it down. It is quite complicated to write it.
In fact, the javascript Number object has a
See a lot of people have this reserved number of decimal point after the demand, but most of them write a function to intercept, but also consider rounding up what, it is quite complicated to write.
In fact, the number object of JavaScript has a
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.