在 C# 中将双精度值四舍五入为整数值
本教程将讨论在 C# 中将双精度值四舍五入为整数值的方法。
使用 C# 中的 Math.Ceiling()
函数将双精度值四舍五入为整数值
如果要将整数值 2.5
舍入为整数值 3
,则必须使用 Math.Ceiling()
函数。Math.Ceiling()
函数将十进制值四舍五入到下一个整数值。下面的代码示例向我们展示了如何使用 C# 中的 Math.Ceiling()
函数将双精度值四舍五入为整数值。
using System;
namespace round_double_to_intt {
class Program {
static void Main(string[] args) {
double d = 2.5;
int i = (int)Math.Ceiling(d);
Console.WriteLine("Original Value = {0}", d);
Console.WriteLine("Rounded Value = {0}", i);
}
}
}
输出:
Original Value = 2.5
Rounded Value = 3
我们使用 C# 中的 Math.Ceiling()
函数将双精度值 2.5
舍入为整数值 3
。这种方法的问题在于,Math.Ceiling()
函数会将小数值 2.3
转换为整数值 3
。
使用 C# 中的 Math.Floor()
函数将双精度值四舍五入为整数值
如果想将双精度值 2.5
舍入为整数值 2
,则必须使用 Math.Floor()
函数。Math.Floor()
函数将一个小数值舍入为前一个整数值。下面的代码示例向我们展示了如何使用 C# 中的 Math.Floor()
函数将双精度值四舍五入为整数值。
using System;
namespace round_double_to_intt {
class Program {
static void Main(string[] args) {
double d = 2.5;
int i = (int)Math.Floor(d);
Console.WriteLine("Original Value = {0}", d);
Console.WriteLine("Rounded Value = {0}", i);
}
}
}
输出:
Original Value = 2.5
Rounded Value = 2
我们使用 C# 中的 Math.Floor()
函数将双精度值 2.5
舍入为整数值 2
。这种方法的问题在于,Math.Floor()
函数会将十进制值 2.9
转换为整数值 2
。
使用 C# 中的 Math.Round()
函数将双精度值四舍五入为整数值
在 C# 中,Math.Round()
函数可用于将双精度值四舍五入最接近的整数值。Math.Round()
函数返回一个双精度值,该值将四舍五入到最接近的整数。下面的代码示例向我们展示了如何使用 C# 中的 Math.Round()
函数将双精度值四舍五入为整数值。
using System;
namespace round_double_to_intt {
class Program {
static void Main(string[] args) {
double d = 2.9;
int i = (int)Math.Round(d);
Console.WriteLine("Original Value = {0}", d);
Console.WriteLine("Rounded Value = {0}", i);
}
}
}
输出:
Original Value = 2.9
Rounded Value = 3
我们使用 C# 中的 Math.Round()
函数将小数值 2.9
舍入为整数值 3
。我们使用类型转换将 Math.Round()
函数返回的双精度值转换为整数值。这种方法只有一个问题。Math.Round()
函数将十进制值 2.5
转换为整数值 2
。
我们可以通过在 Math.Round()
函数的参数中指定 MidpointRounding.AwayFromZero
来解决此问题。下面的代码示例向我们展示了如何使用 C# 中的 Math.Round()
函数将 2.5
舍入为 3
。
using System;
namespace round_double_to_intt {
class Program {
static void Main(string[] args) {
double d = 2.5;
int i = (int)Math.Round(d, MidpointRounding.AwayFromZero);
Console.WriteLine("Original Value = {0}", d);
Console.WriteLine("Rounded Value = {0}", i);
}
}
}
输出:
Original Value = 2.5
Rounded Value = 3
通过在 C# 中的 Math.Round()
函数中指定 MidpointRounding.AwayFromZero
参数,将十进制值 2.5
舍入为整数值 3
。
上面讨论的所有方法在不同的特定情况下都是有用的。将双精度值四舍五入为整数值的最坏方法是通过显式类型转换。这是因为显式类型转换会忽略小数点后的所有值,而只返回小数点前的整数值。
相关文章
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:198 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:171 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:187 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中发出 HTTP POST Web 请求
发布时间:2024/02/04 浏览次数:131 分类:编程语言
-
在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求
在 C# 中运行命令提示符命令
发布时间:2024/02/04 浏览次数:130 分类:编程语言
-
Process 类可用于在 C# 中运行命令提示符命令。在 C# 中使用 Process.Start() 函数运行命令提示符命令
在 C# 中调整图像大小
发布时间:2024/02/04 浏览次数:203 分类:编程语言
-
有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保
在 C# 中下载图片
发布时间:2024/02/04 浏览次数:138 分类:编程语言
-
有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据
在 C# 中使用秒表
发布时间:2024/02/04 浏览次数:139 分类:编程语言
-
我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。
在 C# 中获取可执行路径
发布时间:2024/02/04 浏览次数:200 分类:编程语言
-
有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径