开发者

Displaying decimal values in Qt using qreal Data Type

开发者 https://www.devze.com 2023-03-15 16:24 出处:网络
Is there a way to get decimal values using qreal in Qt?? just like this: qreal decimal; average = 3/2; the output should definitely 1.5 but instead i am getting 1 asresult.

Is there a way to get decimal values using qreal in Qt??

just like this:

qreal decimal;
average = 3/2;

the output should definitely 1.5 but instead i am getting 1 as result.

can anyone 开发者_Python百科help me??


Because everybody is saying how to solve the problem and not what is the problem, here is my answer:

Operations on integers returns integers. To get a real number you should cast one of the operands to a real type.


@andro, you could do (x*1.0)/y or (qreal)x/y


qreal average;
average = 3.0/2;
0

精彩评论

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