简体   繁体   English

转换浮点值

[英]Converting floating-point values

I'm trying to make the program to print out the floating-point value. 我正在尝试使程序打印出浮点值。 I've tried putting %f , and it gave me the same number but with 0 at the end. 我尝试将%f ,它给了我相同的数字,但末尾有0。 In my function definition I tried to make temp double but it keeps saying "expression must have integral type." 在我的函数定义中,我尝试使temp倍增,但它一直说“表达式必须具有整数类型”。

#include <stdio.h> 

int modifyIntegral();

int main() {
    int value;

    value = modifyIntegral();

    printf("The Updated value : %d\n", value);   
}

int modifyIntegral() {
    double value;
    double temp;

    scanf_s("%d", &value);

    temp = (value < 0) ? -value : value;

    if (temp % 10 > temp / 100 % 10) {
        temp += -temp % 10 + temp / 100 % 10;
    }
    else {
        temp += 100 * (temp % 10 - temp / 100 % 10);
    }

    return (value < 0) ? -temp : temp;
}

您不能在双精度上使用模数(%)运算符。

There are many problems in your program. 您的程序中有很多问题。

First, % is an integer operator so you can't use it on floating-point types. 首先, %是整数运算符,因此不能在浮点类型上使用它。 Use fmod() instead. 使用fmod()代替。 But you can't get the digits of the floating-point values by getting the modulo 10 like that without removing the fractional parts 但是,如果不删除小数部分,就无法获得像这样的模数10来获得浮点值的数字

Second, you declared value as double but then read it as an int 其次,您将value声明为double ,然后将其读取为int

scanf_s("%d", &value);

That would cause undefined behavior 那会导致不确定的行为

I don't understand what you need to convert? 我不明白您需要转换什么? Operations like what you did can be done and should be done in integer. 可以像您一样进行操作,并且应该以整数形式进行。

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

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