c#中取整和取余


  • "%"為取余。  
  • "/"號整型運算是取整,浮點運算時為除法運算。如54/10結果為5,54.0/10.0結果為5.4。而且取整時不進行四舍五入只取整數部分,如54/10和56/10是5。  
  • Math.Celling()取整數的較大數,相當於不管余數是什么都會進一位。如Math.Celling(54.0/10.0)結果為6。  
  • Math.Floor()取整數的較小數,相當於"/"號,即不管余數部分是什么都不進行進位。如Math.Floor(56.0/10.0)的結果是5。
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("(54/10):{0}", 54 / 10);
            Console.WriteLine("(56/10):{0}", 56 / 10);
            Console.WriteLine("(54.0%10.0):{0}", 54.0 % 10.0);
            Console.WriteLine("(56.0%10.0):{0}", 56.0 % 10.0);
            Console.WriteLine("Math.Celling(54.0/10.0):{0}", Math.Ceiling(54.0 / 10.0));
            Console.WriteLine("Math.Celling(56.0/10.0):{0}", Math.Ceiling(56.0 / 10.0));
            Console.WriteLine("Math.Floor(54.0/10.0):{0}", Math.Floor(54.0 / 10.0));
            Console.WriteLine("Math.Floor(56.0/10.0):{0}", Math.Floor(56.0 / 10.0));
            Console.ReadKey();
        }
    }
}

(54/10):5
(56/10):5
(54.0%10.0):4
(56.0%10.0):6
Math.Celling(54.0/10.0):6
Math.Celling(56.0/10.0):6
Math.Floor(54.0/10.0):5
Math.Floor(56.0/10.0):5

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM