C # rounding in one method directly obtains the integer digits

Source: Internet
Author: User

[A basket of rice very rare original, reproduced please indicate the source of http://www.cnblogs.com/wanghafan/archive/2012/05/18/2508113.html]

Rounding

View Code

1         public static decimal Round(decimal d, decimal unit)2         {3             decimal rm = d % unit;4             decimal result = d - rm;5             if (rm >= unit / 2)6                 result += unit;7             return result;8         }

 

Method 1

View Code

1         public static decimal Jinyi(decimal d, decimal unit)2         {3             decimal rm = d % unit;4             decimal result = d - rm;5             if (rm > 0)6                 result += unit;7             return result;8         }

 

Directly integer

View Code

1         public static decimal Quzheng(decimal d, decimal unit)2         {3             decimal rm = d % unit;4             decimal result = d - rm;5             return result;6         }

 

 

 

Call Method

View Code

1 int cou = 3; 2 string uit = string. empty; 3 int cou = int. parse (dt. rows [0] ["decimal places"]. toString (); 4 if (cou = 0) 5 {6 uit = "1"; 7} 8 else 9 {10 uit = "0. "; 11 for (int I = 1; I <cou; I ++) 12 uit + =" 0 "; 13 uit + =" 1 "; 14} 15 16 Jinyi (num, decimal. parse (uit); 17 Round (num, decimal. parse (uit); 18 Quzheng (num, decimal. parse (uit ));

 

 

 

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.