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

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

html - Outlook 2010 Anchor (url/address/link) -

android - How to create dynamically Fragment pager adapter -