简体   繁体   English

将浮点数float / double的余数转换为int

[英]Converting remainder of a floating point float/double into an int

I have a double (or float) number x: 我有一个双倍(或浮点数)x:

x = 1234.5678; x = 1234.5678;

Now, the question is, how do break down the number into 2 int's whereas int1 would get the number before the point, and int2 is the number after the point. 现在的问题是,如何将数字分解为2个int,而int1将得到该点之前的数字,而int2是该点之后的数字。

The first part is easy, which we can either cast, or take a round or ceiling to get the first part into an int, I am looking for the second part to become int2=5678 without any floating points there. 第一部分很容易,我们可以铸造,也可以通过圆形或天花板将第一部分变成一个整数,我正在寻找第二个部分成为int2 = 5678而没有任何浮点。

ie to to extend the above example: 即扩展以上示例:

float x = 1234.5678;
int x1 = (int) x;   // which would return 1234 great.
int x2 = SomeFunction????(x);            // where I need x2 to become = 5678

Notice the 5678 should not have any points there. 注意5678那里应该没有任何点。

It would be nice to hear from you. 很高兴收到您的来信。

Thanks Heider 谢谢海德

Here are two ways of doing it. 这有两种方法。

The first one uses std::stringstream , std::string and std::strtol and is sorta hacky. 第一个使用std::stringstreamstd::stringstd::strtol ,并且有点黑。 It is also not very efficient, but it does the job. 它也不是很有效,但是可以完成任务。

The second one needs to know the number of decimals and uses simple multiplication. 第二个需要知道小数位数并使用简单的乘法。 NOTE: This method will not do any kind of checking on whether the float you pass in actually has that number of decimals. 注意:此方法不会对您传递的浮点数是否实际上具有该小数位数进行任何类型的检查。

None of these methods are particularly elegant, but they worked well for the numbers I tested ( both positive and negative. ) Feel free to comment if you find bugs/errors or if you have suggestions for improvement. 这些方法都不是特别出色的方法,但是它们对于我测试的数字(正数和负数)都适用。如果您发现错误/错误或有改进建议,请随时发表评论。

EDIT : As @dan04 pointed out, this method will return the same value for 0.4 as for 0.04 . 编辑:正如@ dan04所指出的,此方法将为0.40.04返回相同的值。 If you want do distinguish these, you'd need a second int for storing the number of zeros after the decimal point. 如果要区分这些,则需要第二个int来存储小数点后的零个数。

#include <iostream>
#include <sstream>
#include <math.h>

int GetDecimalsUsingString( float number );
int GetDecimals( float number, int num_decimals );

int main() {
    float x = 1234.5678;
    int x1 = (int) x;   // which would return 1234 great.
    float remainder = x - static_cast< float > ( x1 );
    std::cout << "Original : " << x << std::endl;
    std::cout << "Before comma : " << x1 << std::endl;
    std::cout << "Remainder : " << remainder << std::endl;

    // "Ugly" way using std::stringstream and std::string
    int res_string = GetDecimalsUsingString( remainder );

    // Nicer, but requires that you specify number of decimals
    int res_num_decimals = GetDecimals( remainder, 5 );

    std::cout << "Result using string : " << res_string << std::endl;
    std::cout << "Result using known number of decimals : " << res_num_decimals << std::endl;
    return 0;
}

int GetDecimalsUsingString( float number )
{
    // Put number in a stringstream
    std::stringstream ss;
    ss << number;

    // Put content of stringstream into a string
    std::string str = ss.str();

    // Remove the first part of the string ( minus symbol, 0 and decimal point)
    if ( number < 0.0 )
        str = str.substr( 3, str.length() - 1);
    else
        str = str.substr( 2, str.length() - 1);

    // Convert string back to int
    int ret =  std::strtol( str.c_str(), NULL, 10 );

    /// Preserve sign
    if ( number < 0 )
        ret *= -1;

    return ret;
}
int GetDecimals( float number, int num_decimals )
{
    int decimal_multiplier = pow( 10, num_decimals );
    int result = number *  decimal_multiplier;

    return result;
}

Output : 输出:

Original : 1234.57
Before comma : 1234
Remainder : 0.567749
Result using string : 567749
Result using known number of decimals : 56774

Ideone Ideone

I guess there are no built in C/C++ commands to do this, other than the 2 methods of: 我想除了以下两种方法外,没有内置的C / C ++命令可以做到这一点:

1) Using the above to convert into string and then scan back into 2 ints. 1)使用上面的方法转换为字符串,然后扫描回2个整数。 2) Accessing the memory contents of the memory variable and then decoding manually. 2)访问存储变量的存储内容,然后手动解码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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