如何转换为整数而不是向下舍入?

时间:2019-01-31 01:24:45

标签: c#

我有此代码:

var t = (timer2Seconds / 10).ToString()

When timer2Seconds is 100 then t is 100
When timer2Seconds is 99 then t is 9

有一种方法可以使它四舍五入,以便:

When timer2Seconds is 99 then t is 10
When timer2Seconds is 91 then t is 10 
When timer2Seconds is 90 then t is 9

1 个答案:

答案 0 :(得分:1)

使用公式:

var d = 10;
var t = (timer2Seconds + d - 1) / d;

它适用于整数并四舍五入。