开发者

Help Rounding division

I need the end result to be a 开发者_运维技巧whole number. If the division is a whole number fine but if it ends up with a decimal, I need to round up (midpoint).

Example if the division is 85.5, round up and result should be 86 If the division is 85.4, it would naturally round down ...basic rounding rules If it's a whole number 85, then we're all good but should I store the result regardless of these 3 situations as type double?

double totalFoundPercent = Math.Round(
        (double) (totalNumbersDeleted / (totalNumbers - totalNumbersApproved)), 
                  MidpointRounding.AwayFromZero);

This is giving me an error of:

The call is ambiguous between the following methods or properties: 'System.Math.Round(double, System.MidpointRounding)' and 'System.Math.Round(decimal, System.MidpointRounding)'

So I am not sure if this should be stored as a type double in the end or not plus the problem with the error.


Once you have rounded a number, you have the choice of storing it as a integer or long as appropriate. You could choose to store it as a floating point type such as float or double but as you know it has no fractional part it would probably be clearer to a reader to see it represented as an integral type.

One thing to bear in mind is that floating point numbers are able to imprecisely store very large numbers which may not fit into a regular int or even long type. The biggest long is 9,223,372,036,854,775,807 whereas the biggest double is 1.7976931348623157 × 10³⁰⁸.

The error, however is confusing as it should patently be calling the double overload: there is no implicit conversion between double and decimal and your cast makes the type very clear. Are you sure you are getting this error with the exact code you have in your question?


Get rid of the second parameter. Based on your description, you shouldn't need it.

double totalFoundPercent = Math.Round((double) (totalNumbersDeleted / (totalNumbers - totalNumbersApproved)));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜