C# Double Rounding

2019-07-27 10:27发布

问题:

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.



标签: c# java double