C++ floating point to integer type conversions

前端 未结 8 2216
长发绾君心
长发绾君心 2020-12-01 08:50

What are the different techniques used to convert float type of data to integer in C++?

#include 

using namespace std;
struct database {
  i         


        
相关标签:
8条回答
  • 2020-12-01 09:26

    For most cases (long for floats, long long for double and long double):

    long a{ std::lround(1.5f) }; //2l
    long long b{ std::llround(std::floor(1.5)) }; //1ll
    
    0 讨论(0)
  • 2020-12-01 09:27

    What you are looking for is 'type casting'. typecasting (putting the type you know you want in brackets) tells the compiler you know what you are doing and are cool with it. The old way that is inherited from C is as follows.

    float var_a = 9.99;
    int   var_b = (int)var_a;
    

    If you had only tried to write

    int var_b = var_a;
    

    You would have got a warning that you can't implicitly (automatically) convert a float to an int, as you lose the decimal.

    This is referred to as the old way as C++ offers a superior alternative, 'static cast'; this provides a much safer way of converting from one type to another. The equivalent method would be (and the way you should do it)

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

    This method may look a bit more long winded, but it provides much better handling for situations such as accidentally requesting a 'static cast' on a type that cannot be converted. For more information on the why you should be using static cast, see this question.

    0 讨论(0)
  • 2020-12-01 09:29

    the easiest technique is to just assign float to int, for example:

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

    this will truncate everything behind floating point or you can round your float number before.

    0 讨论(0)
  • 2020-12-01 09:32

    One thing I want to add. Sometimes, there can be precision loss. You may want to add some epsilon value first before converting. Not sure why that works... but it work.

    int someint = (somedouble+epsilon);
    
    0 讨论(0)
  • 2020-12-01 09:35

    Size of some float types may exceed the size of int. This example shows a safe conversion of any float type to int using the int safeFloatToInt(const FloatType &num); function:

    #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;
    }
    

    Result:

    Unsafe conversion of value:2.2518e+15
    2147483647
    43
    23333
    
    0 讨论(0)
  • 2020-12-01 09:40

    I believe you can do this using a cast:

    float f_val = 3.6f;
    int i_val = (int) f_val;
    
    0 讨论(0)
提交回复
热议问题