C# 之 Math取整数 您所在的位置:网站首页 四舍五入到整百的函数是什么 C# 之 Math取整数

C# 之 Math取整数

2024-07-11 23:26| 来源: 网络整理| 查看: 265

C# 之 Math取整数 引用

主要用到 System 命名空间下的一个数据类 Math ,调用他的方法。

C#取整函数使用详解: 1、Math.Round是"就近舍入",当要舍入的是5时与"四舍五入"不同(取偶数),如:

Math.Round(0.5,0)=0 Math.Round(1.5,0)=2 Math.Round(2.5,0)=2 Math.Round(3.5,0)=4

2、Math.Truncate 计算双精度浮点数的整数部分,即直接取整数,如:

Math.Truncate(-123.55)=-123, Math.Truncate(123.55)=123

3、Math.Ceiling 取天板值,即向上取整,与"四舍五入"无关。

Math.Ceiling(1) = 1 Math.Ceiling(1.1) = 2 Math.Ceiling(1.5) = 2 Math.Ceiling(3.1) = 4

4、Math.Floor 取地板值,即向下取整,与"四舍五入"无关。

Math.Floor(1) = 1 Math.Floor(1.1) = 1 Math.Floor(1.5) = 1 Math.Floor(3.9) = 3

取天板值与地板值,与"四舍五入"无关。其实Floor的结果与(int)相同,因此也可以这样写

Math.Floor((double)2/3+0.5) int a = 5; int b = 2; lbl.Text = Convert.ToString(Math.Ceiling((double)a / (double)b)); 引用

[1]: C# 之 Math取整 - Now,DayBreak - 博客园 http://www.cnblogs.com/xinaixia/p/4834271.html



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有