获取数字的小数部分 [英] Get decimal part of a number

查看:78
本文介绍了获取数字的小数部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图将整数的整数和小数部分分成两个变量.我尝试过的:

Trying to get whole and decimal part of a number into two variables. What I tried:

#include <iostream>
int main(){
    float n,m; int k; std::cin >> n;
    k = n;
    m = n - k;

试图将浮点数转换为int并收到编译器的警告,该数可能不正确,经过测试,确实不正确并且无法获得预期的结果.对此进行搜索,除了使用 floor()之外,找不到其他解决方法.

Tried to convert the float number to int and received warning by the compiler that the number could be incorrect, tested, was incorrect indeed and couldn't get the expected results. Searched on this, couldn't find any other workaround than using floor().

我的实际代码:

int main() {
    float n; cin >> n;
    int nfloor = n;
    cout << nfloor << "\n";
    float nfloat = n - nfloor; int nfloatfloor;
    cout << nfloat << "\n";
    do {
        nfloat *= 10;
        nfloatfloor = nfloat;
        cout << nfloat << "\n" << nfloatfloor << "\n";
    } while (nfloat > nfloatfloor);
}

结果:

Input: 12.34
Output :
12
0.34
3.4
3
34
34
340
340
3400
3400
34000
34000
340000
340000
3.4e+06
3400001
3.4e+07
34000016

将两个浮点数相减会返回错误的值,对此进行了搜索,但答案是我无法理解的高水平.

Subtracting two float numbers returns an incorrect value, Searched on this but the answers were on a high level that I couldn't understand.

我的实际代码:

int main() {
    float n; cin >> n;
    float nfloor = floor(n);
    cout << nfloor << "\n";
    float nfloat = n - nfloor; float nfloatfloor;
    cout << nfloat << "\n";
    do {
        nfloat *= 10;
        nfloatfloor = floor(nfloat);
        cout << nfloat << "\n" << nfloatfloor << "\n";
    } while (nfloat > nfloatfloor);
}

结果:

Input: 12.34
Output:
12
0.34
3.4
3
34
34 //Run should stop here because of the while loop bit it doesn't, funny thing is it gives me different results sometimes, last time it gave me 35 and 34
340
340
3400
3400
34000
34000
340000
340000
3.4e+06
3.4e+06
3.4e+07
3.4e+07

@Slava看一下这句话上方的输出,编译器分别输出34和34,重复的回答表明,cout是34.0000000000000004或类似的东西,正如我在上面的评论中所述,代码应该已经停止了,我我真正想做的是比较一个浮点数和一个int数,如果(float> int)该代码应该继续,如果不应该停止,那么有什么解决办法吗?@hnefatl我尝试了您的答案,编译器挂起:

@Slava Take a look at the output right above this sentence, compiler printed 34 and 34, the duplicate answer shows that the couts are 34.0000000000000004 or something like that, and as I commented above, the code should have stopped, what I'm really trying to do is compare a float number and int number, if (float >int) the code should continue and if not it should stop so is there any solution? @hnefatl I tried your answer and the compiler just hangs:

int main() {
    float n2, whole, fractional, fractional2, whole2; cin >> n2;
    int denominator = 1;
    fractional = modf(n2, &whole);
    do {
        fractional *= 10;
        fractional2 = modf(fractional, &whole2);
        denominator *= 10;
    } while (fractional > fractional2);
    if (denominator > 1)
        denominator /= 10;
    cout << denominator;
}

推荐答案

为什么不使用 std :: modf ,用于此目的:

Why not use std::modf, which is designed for this purpose:

float n = 12.34;
float whole, fractional;

fractional = std::modf(n, &whole);

该值的非小数部分在整个中,而小数部分在 fractional 中.

The non-fractional part of the value is in whole while the fractional part is in fractional.

如果您随后希望获得整个部分的整数值(请记住,由于 float 的范围可能大于 int ),您可以执行以下操作:

If you then want to get an integer value for the whole part (bearing in mind that you can lose data this way as the range of a float can be larger than that of an int), you can just do:

int integralWhole = static_cast<int>(whole);

这篇关于获取数字的小数部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆