C++ 浮点到整数类型转换

问题描述 投票:0回答:9

在 C++ 中将浮点型数据转换为整数有哪些不同的技术?

#include <iostream>

using namespace std;
struct database {
  int id, age;
  float salary;
};

int main() {
  struct database employee;
  employee.id = 1;
  employee.age = 23;
  employee.salary = 45678.90;
  /*
     How can i print this value as an integer
     (with out changing the salary data type in the declaration part) ?
   */
  cout << endl << employee.id << endl << employee.
  age << endl << employee.salary << endl;
  return 0;
}
c++ floating-point integer type-conversion typecast-operator
9个回答
101
投票

您正在寻找的是“类型转换”。类型转换(将您想要的类型放在括号中)告诉编译器您知道自己在做什么并且对此很满意。从C继承的旧方法如下。 float var_a = 9.99; int var_b = (int)var_a;

如果你只是尝试写作

int var_b = var_a;

您会收到一条警告,提示您无法隐式(自动)将 
float

转换为

int
,因为您会丢失小数。

这被称为旧方法,因为 C++ 提供了更好的替代方法“静态转换”;这提供了一种更安全的从一种类型转换为另一种类型的方法。等效的方法是

(以及你应该这样做的方式)

float var_x = 9.99; int var_y = static_cast<int>(var_x);

此方法可能看起来有点冗长,但它为诸如意外请求对无法转换的类型进行“静态转换”等情况提供了更好的处理。有关为什么应该使用静态转换的更多信息,请参阅
这个问题


39
投票

float f = 3.4; int n = static_cast<int>(f); // n = 3



18
投票
int

的大小。 此示例显示使用

int
函数将任何浮点类型安全转换为
int safeFloatToInt(const FloatType &num);

#include <iostream> #include <limits> using namespace std; template <class FloatType> int safeFloatToInt(const FloatType &num) { //check if float fits into integer if ( numeric_limits<int>::digits < numeric_limits<FloatType>::digits) { // check if float is smaller than max int if( (num < static_cast<FloatType>( numeric_limits<int>::max())) && (num > static_cast<FloatType>( numeric_limits<int>::min())) ) { return static_cast<int>(num); //safe to cast } else { cerr << "Unsafe conversion of value:" << num << endl; //NaN is not defined for int return the largest int value return numeric_limits<int>::max(); } } else { //It is safe to cast return static_cast<int>(num); } } int main(){ double a=2251799813685240.0; float b=43.0; double c=23333.0; //unsafe cast cout << safeFloatToInt(a) << endl; cout << safeFloatToInt(b) << endl; cout << safeFloatToInt(c) << endl; return 0; }

结果:

Unsafe conversion of value:2.2518e+15 2147483647 43 23333



8
投票

long a{ std::lround(1.5f) }; //2l long long b{ std::llround(std::floor(1.5)) }; //1ll



3
投票
NumericConversion

库。它将允许显式控制您想要如何处理溢出处理和截断等问题。


1
投票

float f_val = 3.6f; int i_val = (int) f_val;



1
投票

int i; float f; f = 34.0098; i = f;

这将截断浮点数后面的所有内容,或者您可以在之前对浮点数进行四舍五入。


1
投票

int someint = (somedouble+epsilon);



0
投票

#define EXPONENT_LENGTH 8 #define MANTISSA_LENGTH 23 // to convert float to int without floating point operations int ownFloatToInt(int floatBits, int scaler) { int sign = (floatBits >> (EXPONENT_LENGTH + MANTISSA_LENGTH)) & 1; int exponent = (floatBits >> MANTISSA_LENGTH) & ((1 << EXPONENT_LENGTH) - 1); int mantissa = (floatBits & ((1 << MANTISSA_LENGTH) - 1)) | (1 << MANTISSA_LENGTH); int result = mantissa * scaler; // possible overflow exponent -= ((1 << (EXPONENT_LENGTH - 1)) - 1); // exponent bias exponent -= MANTISSA_LENGTH; // modify exponent for shifting the mantissa if (exponent <= -(int)sizeof(result) * 8) { return 0; // underflow } if (exponent > 0) { result <<= exponent; // possible overflow } else { result >>= -exponent; } if (sign) result = -result; // handle sign return result; }

© www.soinside.com 2019 - 2024. All rights reserved.