zoukankan      html  css  js  c++  java
  • 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;
     using System.Text;
    namespace ConsoleApplication5
     { 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)); } } }
    C#中,关于除法"/"运算得一点问题。
      现在C#与法中,"/"除后所得的值的类型,跟他的除数和被除数的类型有关。如: int a=4; int b=5; float c=a/b ;   则结果为0(因为会先进行int的除法操作,得出结果0,再将结果转为float 0;);   总之,得出的数都是整形的,最终发觉原来除后所得的值的类型,跟他的除数和被除数的类型有关。所以,应写成: float a=3; float b=5; float c=a/b; 这样,才能得出正确的结论!
  • 相关阅读:
    第十三章 部署Java应用程序
    分布式系列五: RMI通信
    分布式系列四: HTTP及HTTPS协议
    分布式系列三: 对象序列化
    程序中的 “负数取模” 问题
    【转】Linux C函数库参考
    【转】 Linux中记录终端输出到txt文本文件
    【转】 #define用法详解
    error: ‘to_string’ was not declared in this scope
    exit() 与 return() 的区别
  • 原文地址:https://www.cnblogs.com/cuihongyu3503319/p/4750890.html
Copyright © 2011-2022 走看看