如何在C#中获得舍入值? [英] How to get rounded Off value in C#?

查看:101
本文介绍了如何在C#中获得舍入值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望圆角值的舍入值如下:

如果我舍入5.51 - >到6.00

我想要舍入的值(这里.49我怎么得到这个值?),



如果我舍入5.48 - - >到5.00

我想要舍入的值(这里.48我怎么得到这个值?)



请帮帮我..



在此先感谢....

I want the rounded off value from the rounded value like :
If I rounded 5.51 --> to 6.00
I want the value which rounded(Here .49 how i get this value?),
and
If I rounded 5.48 --> to 5.00
I want the value which rounded(Here .48 how i get this value?)

Please Help me out..

Thanks In advance....

推荐答案

首先,您应该从中查看问题另一面思考为什么你需要四舍五入。我不相信你真的需要它。这很少需要。 (我所知道的一个罕见的排除是生成伪随机数;我怀疑你是在开发类似新的随机数生成器。:-))



很多更有可能的是你只是想在屏幕上显示一个舍入值,这是非常不同的。在这种情况下,您应该考虑格式化,而不是舍入自身。请参阅:

http://msdn.microsoft.com/en-us /library/dwhawy9k.aspx [ ^ ],

http://msdn.microsoft.com/en -us / library / 0c899ak8.aspx [ ^ ]。



顺便说一下,即使你找到了使用舍入来解决问题的正确方法,也要再想一想。避免这样做更加安全,只专注于格式化。即使意外,你也不想四舍五入到你的计算中。



祝你好运,

-SA
First of all, you should look at the problem from a different side and think why do you need rounding. I don't believe you really need it. It is very rarely needed. (One rare exclusion I known is generation of pseudo-random numbers; and I doubt that you are developing something like a new random number generator. :-))

Much more likely that you simply want to show a rounded value on screen, and this is something very different. In this case, you should think about formatting, but not rounding itself. Please see:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

By the way, even if you find a correct way to solve the problem using rounding, think again. It's much safer avoid doing it and focusing on formatting only. You don't want rounding to sneak into your calculations even by accident.

Good luck,
—SA


按照答案在C#中舍入十进制值 [ ^ ]。

Follow the answer Round Off decimal values in C#[^].
引用:

查看 Math.Round(十进制) [< a href =http://msdn.microsoft.com/en-us/library/3s2d3xkk.aspxtarget =_ blanktitle =New Window> ^ ]或带有MidpointRounding参数的重载。 [ ^ ]



当然,你需要解析并格式化值以从/到文本。如果这是用户输入的输入,则应该使用decimal.TryParse,使用返回值来确定输入是否有效。



Look at Math.Round(decimal)[^] or the overload which takes a MidpointRounding argument.[^]

Of course, you'll need to parse and format the value to get it from/to text. If this is input entered by the user, you should probably use decimal.TryParse, using the return value to determine whether or not the input was valid.

string text = "19500.55";
decimal value;
if (decimal.TryParse(text, out value))
{
    value = Math.Round(value);
    text = value.ToString();
    // Do something with the new text value
}
else
{
    // Tell the user their input is invalid
}





你好,你可以做像这样:



Hi you can do like this:

double a = 5.50;
string[] str= a.ToString().Split('.');
double num1= Convert.ToDouble("0."+str[1]);
double res;
if (num1 < 0.51)
{
     res= Math.Floor(a);
}
else {
    res = Math.Round(a); 
}


这篇关于如何在C#中获得舍入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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