Five homes and six homes → five homes

Source: Internet
Author: User

M $ is always a leader. Math. Round () in C # seems to be not so useful in round-robin calculation. It can be said that it is "five homes and six in ~~~ I didn't know this at the beginning, and the calculation results were always incorrect. Later, I followedProgramThis method fooled me. No way. Since it is so difficult to use or have no Chinese characteristics, you can only create some Chinese characteristics on your own. Override round ():

Public   Static   Double Round ( Double D, Int I)
{
If (D > = 0 )
{
D+ = 5 *Math. Pow (10,-(I+ 1));
}
Else
{< br> d + = - 5 * math. pow ( 10 , - (I + 1 ));
}
String Str = D. tostring ();
String [] STRs = Str. Split ( ' . ' );
Int Idot = Str. indexof ( ' . ' );
String Prestr = STRs [ 0 ];
String Poststr = STRs [ 1 ];
If (Poststr. Length > I)
{
Poststr=Str. substring (idot+ 1, I );
}
String Strd = Prestr +   " . "   + Poststr;
D = Double. parse (strd );
Return D;
}

Parameter: D indicates the number to be rounded to, And I indicates the number of decimal places to be retained.

Initially, the results are not bad. I thought about all the questions in the book. OK! That's all. Don't affect the accuracy of GM.

Finally, let's take an ion and describe the principle:
If you want to round 1.4 and 1.5 to an integer, add 0.5 first, and then the string of digits before the decimal point is OK. We can see that 1.4 + 0.5 = 1.9 = get 1; 1.5 + 0.5 = 2.0 = Get 2. This is a perfect idea!

PS: It's a pity that I didn't come up with a good idea! Thanks to the unsung hero on the Internet ~~~ It's a little comforting for me to make good ideas and implement them into action...

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.