舍入功能问题c#.net [英] Rounding functionality problem c#.net

查看:57
本文介绍了舍入功能问题c#.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在该函数中有一个计算函数,其值为21.1049



使用Math.Round(值,2)后



i想要输出值= 21.11



我怎么能这样做,请告诉我解决方案?



我尝试了什么:



我试过这个

double a = 21.1049;



//但是给出21.10输出

double b = Math.Round(a,2);

b COnvert.ToDouble(a.ToStrin(0.00);

b COnvert.ToDouble(a.ToStrin(0。##);

b = Math.Round(a,2,MidpointRounding.AwayFromZero);

b = Math.Round(a,2,MidpointRounding.ToEven);

b = a。 ToString(N2);



都给我输出21.10

I have a calculation function in that function having value 21.1049

After using Math.Round(value,2)

i want the out put value = 21.11

How can i do this, please suggest me the solution?

What I have tried:

I have tried this
double a = 21.1049;

// but is gives 21.10 out output
double b = Math.Round(a,2);
b COnvert.ToDouble(a.ToStrin("0.00");
b COnvert.ToDouble(a.ToStrin("0.##");
b = Math.Round( a ,2,MidpointRounding.AwayFromZero);
b = Math.Round( a ,2,MidpointRounding.ToEven);
b = a.ToString("N2");

all are giving me the output 21.10

推荐答案

避免使用转换除非真的需要,这是一种罕见的情况。在你的情况下,根本不需要它。而是使用 Parse TryParse 您需要使用的基元类型的方法。您所做的实际上是解析,而不是转换。例如,对于double,请参阅:

Double.Parse方法(系统) [ ^ ],

Double.TryParse方法(系统) [ ^ ]。



现在,我几乎无法相信你真的需要四舍五入。同样,您很少需要直接使用舍入。如果你这样做,那么你很可能会造成精度损失,就是这样。不应该舍入中间结果。只有当您需要显示一定数量的屏幕时才需要舍入输出。但在这种情况下,这不是直接舍入,这是格式化:

Double.ToString方法(系统) [ ^ ],

标准数字格式字符串 [ ^ ],

自定义数字格式字符串 [ ^ ]。



-SA
Avoid using Convert unless it is really needed, which is a rare case. In your case, you don't need it at all. Instead, use Parse or TryParse methods for the primitive types you need to use. What you do is really parsing, not "conversion". For example, for double, please see:
Double.Parse Method (System)[^],
Double.TryParse Method (System)[^].

Now, I hardly can believe that you really need rounding. Again, you rarely need to use rounding directly. If you do, you mostly risk to create loss of precision, that's it. No intermediate results should be rounded. Rounding is only needed for output, say, when you need to show some number of screen. But in this case, this is not directly rounding, this is formatting:
Double.ToString Method (System)[^],
Standard Numeric Format Strings[^],
Custom Numeric Format Strings[^].

—SA


感谢您的建议和答案我经过大量的搜索和反复试验后得到了答案,我得到了wh我想要的是,所以我使用以下解决方案。

再次为你们提供帮助。



Thanks for your suggesions and answers i got the answer after lots of searching and trial and error , I got whatever i want, so i use the following solution.
Once again for than you guys for your help.

public static Double myRound(Double Value, int places = 1000)
{
    Double myvalue = (Double)Value;
    if (places == 1000)
    {
        if (myvalue - (int)myvalue == 0.5)
        {
            myvalue = myvalue + 0.1;
            return (Double)Math.Round(myvalue);
        }
        return (Double)Math.Round(myvalue);
        places = myvalue.ToString().Substring(myvalue.ToString().IndexOf(".") + 1).Length - 1;
    } if ((myvalue * Math.Pow(10, places)) - (int)(myvalue * Math.Pow(10, places)) > 0.49)
    {
        myvalue = (myvalue * Math.Pow(10, places + 1)) + 1;
        myvalue = (myvalue / Math.Pow(10, places + 1));
    }
    return (Double)Math.Round(myvalue, places);
}




我建议你的案子使用:

b = Math.Ceiling(a * 100.0)/100.0;



祝你好运
Hi,
I propose for your case to use:
b = Math.Ceiling(a*100.0)/100.0;

Best regards


这篇关于舍入功能问题c#.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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