In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What is the role of Math.Round in C #, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
In order to round off 2 decimal places, the developer wrote the following code
Var num = Math.Round (12.125, 2)
The code is very simple, and the developer actually gets 12.12, which is contrary to the expected rounding result of 12.13.
In fact, the reason for this result is that Math.Round does not use the principle of rounding by default, but the principle of rounding 50 pairs.
Rounded up to 50 pairs.
The so-called rounding 50 pairs means that when the significant digit is determined, the next significant digit is dropped if it is less than or equal to 4, if it is greater than or equal to 6, and when the next significant digit is 5
If 5 is preceded by an odd number, it is rounded into one. If 5 is preceded by an even number, it is not rounded (0 is an even number).
Statistically speaking, rounding 50 pairs is more accurate than rounding, because in the case of a large number of calculations, rounding every five into one will lead to a bias towards large numbers.
For example:
1.15, 1.25, 1.35, 1.45 = 5.2
If the number of significant digits is one decimal place, the result obtained by using the rounding principle
1.2 + 1.3 + 1.4 + 1.5 = 5.4
The result of using the principle of rounding and rounding 50% pairs is
1.2 + 1.2 + 1.4 + 1.4 = 5.2
Thus it can be seen that the result obtained by the principle of rounding and rounding 50% is more accurate.
Rounding of Math.Round
So how do you use Math.Round to achieve the expected rounding?
In fact, Math.Round in C # provides a lot of overloaded methods, two of which are
Public static double Round (double value
Int digits
MidpointRounding mode)
Public static decimal Round (decimal d
Int decimals
MidpointRounding mode)
Both methods provide a third parameter, mode. Mode is an enumerated variable of MidpointRounding, which has two optional values.
AwayFromZero-rounding ToEven-rounding 50 pairs
So if we want an ideal rounding result, we can use the following code instead:
Var num = Math.Round (12.125, 2
(MidpointRounding.AwayFromZero): is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.