c# - MidpointRounding.AwayFromZero -
why below code returns 1299.49 ? according msdn documentation should give 1299.5.
console.writeline(math.round( 1299.492, 2, midpointrounding.awayfromzero))
you may confused how overload works. according msdn on midpointrounding:
when number halfway between 2 others, rounded toward nearest number away zero.
in case, 1299.492 not halfway between 1229.49 , 1299.50, midpointrounding.awayfromzero
doesn't apply.
it looks you're trying round up nearest 2 decimal places. in case, want this answer:
public static double roundup(double input, int places) { double multiplier = math.pow(10, convert.todouble(places)); return math.ceiling(input * multiplier) / multiplier; }
this rounds specified decimal places multiplying 10^places
(100 if need 2 places), calling math.ceiling
, , dividing.
this works:
console.writeline(roundup(1299.492, 2)); // 1299.5
Comments
Post a Comment