简体   繁体   English

C++ 将 double 转换为 long

[英]C++ converting double to long

我在程序中使用了pow(double base, double exponent) ,如何将答案转换为long数据类型?

Just use one of the following statements (depending on your personal requirements):只需使用以下语句之一(取决于您的个人要求):

  1. lround(pow(base, exp)); (nearest number to the result) (最接近结果的数字)
  2. floor(pow(base, exp)); (nearest number less than the result) (小于结果的最近数)
  3. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM