C#-四舍五入到最接近的整数 [英] C# - Rounding Down to Nearest Integer

查看:54
本文介绍了C#-四舍五入到最接近的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在计算一些数字的C#应用​​程序.我需要四舍五入.

I have a C# app that is calculating some numbers. I need to round down.

var increment = 1.25;
var result = 50.45 - 23.70;    // equals 26.75
int interval = difference / increment; // result is 21.4. However, I just want 21

我必须将 interval 设置为 int .同时,由于

I have to get the interval to an int. At the same time, I cannot just use Convert.ToInt32 because of its rounding behavior. I always want the lowest whole number. However, I'm not sure how.

推荐答案

只需尝试一下.

 int interval = Convert.ToInt32(Math.Floor(different/increment));

这篇关于C#-四舍五入到最接近的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆