开发者

Looking for QT function to round a variable (integer) of type qint64 to the nearest ten

开发者 https://www.devze.com 2023-04-11 10:18 出处:网络
I\'m looking for a QT function to round a variable (integer) of type qint64 to the nearest ten. For example:

I'm looking for a QT function to round a variable (integer) of type qint64 to the nearest ten.

For example: 1013 would round to 1010. 1019 would round to 1020

QT Assistant doesn't seem to list any built in functions that would do this, but I could be looking in the wrong spot.

Any help would be appreciated.

Thanks, 开发者_如何学编程Wes


You may try this old trick for positive numbers, replace + with - for negative:

i_rounded = 10 * ((i + 5) / 10);


The normal way to do this with integer (truncating) math is 10*((n+5)/10). That is for a positive number, of course.

n = 17:
17 + 5 = 22
22 / 10 = 2    // integer math truncates
2 * 10 = 20

n = 12:
12 + 5 = 17
17 / 10 = 1
1 * 10 = 10

For negative, add negative 5. So the formula is really …+sign(n)*5 where sign returns -1, 0, or 1 depending on the sign of the number.


I'm not a very familiar with QT but what about

round(value/10)*10

does it work?

p.s: if value is integer, it may need to be converted to double before division.

0

精彩评论

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

关注公众号