Rounding Algorithm for decimal places

Source: Internet
Author: User

Description:

Only a maximum of six decimal places can be rounded in. Note that you can take the initiative to go to 0 at the end of the decimal point (for example, 1.20f is considered as: 1.2f ), if the input decimal point is 2.0f, 0 is returned as an integer;

Resolved issues:

Rounding the mathf. Round Function in the unity3d holy bookReturns the nearest integer rounded to the floating point F.Assume that the number ends with. 5, so it is in the middle of two integers,Whether it is an even number or an odd number, an even number is returned..

Code: [C #, unity3d Holy Book]

Float myround (float f) {string S = f. tostring (); string [] S0 = S. split (New char [] {'. '}); If (s0.length = 1) {print ("it is a integer"); Return 0;} If (s0.length! = 2) {print ("Something is wrong"); Return 0;} If (S0 [1]. length = 1) // If (Int. parse (S0 [1] [0]. tostring ()> = 5) return mathf. floortoint (f) + 1; elsereturn mathf. floortoint (f);} float power = 10; for (INT I = 2; I <S0 [1]. length; I ++) // S0 [1]. length indicates the number of digits in the decimal part {power * = 10;} f * = power; F = mathf. floortoint (f); If (Int. parse (S0 [1] [S0 [1]. length-1]. tostring ()> = 5) F + = 1; F/= power; return F ;}


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.