数学取整要求 [英] Math Rounding Requirement
问题描述
我有一个要求,其中四舍五入为.5及以下的小数,并四舍五入为.6及以上.让我用一个例子来解释你.
13.5-> 13.0是期望值
13.6-> 14.0是期望值
现在我应该编写代码还是在c#中有任何内置功能.<br/>
请确认
意思是
13.4-> 13是期望值
13.5-> 13是期望值
13.6-> 14是期望值
请确认我是否必须编码或是否有任何内置功能
Hi,
I have a requirement where decimal with .5 and less should be rounded down and decimal with .6 and above should be rounded up. Let me explain you with an example.
13.5 --> 13.0 is the expected value
13.6 --> 14.0 is the expected value
Now should i have to code it or is there any built in functionality in c#.
Please confirm
What is meant is
13.4 --> 13 is the expected value
13.5 --> 13 is the expected value
13.6 --> 14 is the expected value
Please confirm if i have to code or is there any in built functionality
推荐答案
double num = 5.5;
int intpart = (int)num;
double decpart = num - intpart;
if (decpart > .5)
{
intpart = intpart + 1;
}
这是你的答案
快乐的编码;)
here is your answer
happy coding ;)
Math.Round [ ^ ]
尝试这个逻辑..
Try this logic..
double val = Convert.ToDouble(textBox1.Text);
MessageBox.Show(Math.Round((val % .5) == 0 ? (val - 0.1) : val).ToString());
这篇关于数学取整要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!