[英]C++ converting double to long
我在程序中使用了pow(double base, double exponent)
,如何将答案转换为long
数据类型?
Just use one of the following statements (depending on your personal requirements):只需使用以下语句之一(取决于您的个人要求):
lround(pow(base, exp));
(nearest number to the result) floor(pow(base, exp));
(nearest number less than the result) ceil(pow(base, exp));
(nearest number larger than the result) I was looking for an actual conversion from double to long.我正在寻找从 double 到 long 的实际转换。
After some research, casting actually does the trick:经过一些研究,转换实际上可以解决问题:
double myDouble = 12.345;
long myTruncatedLong = (long) myDouble;
myTruncatedLong will be 12. myTruncatedLong 将为 12。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.