c++ - 将 float float/double 的余数转换为 int

标签 c++ floating-point int decimal-point

我有一个 double (或 float )x:

x = 1234.5678;

现在的问题是,如何将数字分解为 2 个整数,而 int1 将得到点之前的数字,而 int2 是点之后的数字。

第一部分很简单,我们可以对其进行转换,或者进行一轮或上限将第一部分转换为 int,我希望第二部分成为 int2=5678,其中没有任何 float 。

即扩展上面的例子:

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

注意 5678 不应该有任何点。

很高兴收到你的来信。

谢谢 黑德

最佳答案

这里有两种方法。

第一个使用 std::stringstreamstd::stringstd::strtol 有点老套。它也不是很有效,但它完成了工作。

第二个需要知道小数位数并使用简单的乘法。注意:此方法不会对您传入的 float 是否实际具有该小数位数进行任何类型的检查。

这些方法都不是特别优雅,但它们对我测试的数字(正面和负面)都很有效。如果您发现错误/错误或有改进建议,请随时发表评论。

编辑:正如 @dan04 所指出的,此方法将为 0.4 返回与 0.04 相同的值。如果你想区分这些,你需要第二个 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;
}

输出:

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

Ideone

关于c++ - 将 float float/double 的余数转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21055011/

相关文章:

c++ - 使用 std::find 在 std::vector 中查找 std::tuple

c++ - 如何比较 time_t 和 std::filesystem::file_time_type

c - 如何检查c中是否为 "set"

java - "error: int cannot be dereferenced"尝试从 array.length 中减 1

c++ - 如何处理不同 Linux 发行版上的不同头文件位置?

c - 从捕获浮点异常返回

binary - 是否可以将最高有效的十进制数字精度转换为二进制并返回十进制而又不丢失有效值6或7.225?

c++ - 比较两个 double 的正负

android - Android 中的自定义字体 - 简单而便宜的方法

给定类型列表的 C++ 专用模板类