目前,我正在学习C语言和语言的基础知识,但我的代码有问题。当我把两个数字相乘时,我不能得到小数,即使我把输入的数字浮起来。
我的代码:

int main()
{
    double result_met, km;

    result_met = km = 0.0f;

    /*Display text*/
    printf("Enter values of the distance between the two cities in km's\n");
    scanf_s("%d", &km);

    /*Formular for a simple km conversion*/
    result_met = km * 1000.0f;

    /*Result print*/
    printf("Meter:%d", result_met);

    printf("\nWaiting for a character to be pressed from the keyboard to exit.\n");

    getch();

    return 0;
}

最佳答案

格式说明符不正确-它应该是%lf-%d用于int

scanf_s("%lf", &km);

/*Formular for a simple km conversion*/
result_met = km * 1000.0f;

/*Result print*/
printf("Meter:%lf", result_met);

Format string specifications

关于c - C编程-以小数形式输出结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22249970/

10-11 19:44