How to round a integer to the close hundred?

前端 未结 9 1216
生来不讨喜
生来不讨喜 2020-11-29 08:57

I don\'t know it my nomenclature is correct! Anyway, these are the integer I have, for example :

76
121
9660

And I\'d like to round them to

9条回答
  •  广开言路
    2020-11-29 09:23

    Just some addition to @krizzzn's accepted answer...

    Do note that the following will return 0:

    Math.Round(50d / 100d, 0) * 100;
    

    Consider using the following and make it return 100 instead:

    Math.Round(50d / 100d, 0, MidpointRounding.AwayFromZero) * 100;
    

    Depending on what you're doing, using decimals might be a better choice (note the m):

    Math.Round(50m / 100m, 0, MidpointRounding.AwayFromZero) * 100m;
    

提交回复
热议问题