C# Double Rounding

夙愿已清 提交于 2019-12-11 06:28:55

问题


I'm currently working on a program and im converting my java code into c# . but i'm having some trouble.

public double round(double value){
        BigDecimal b = new BigDecimal(value);
        b = b.setScale(2,BigDecimal.ROUND_UP);
        return (b.doubleValue());
    }

i wrote this converting code but i cant convert it to c#.BigDecimal type causes some problem and im totally new to .Net.Definitely need some help.

Edit : Ok buds i got it , sorry for the dumb question.


回答1:


Couldn't you just do this to round to 2 fractional digits?

        double foo = 3.143;
        double fooRounded = Math.Round(foo, 2); 



回答2:


Here is a C# method that you can use instead:

public double round(double value){
    return Math.Round(value, 2, MidpointRounding.AwayFromZero);   
}

.Net's MidpointRounding.AwayFromZero is the equivalent of java's ROUND_UP.



来源:https://stackoverflow.com/questions/4811488/c-sharp-double-rounding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!