简体   繁体   English

关于浮点数

[英]Regarding Floating Point Numbers

I know "homework" isn't perceived well on here but I've tried searching online but I'm not really getting anywhere and I'm not asking anyone to complete it, just to point me in the right direction. 我知道“作业”在这里并不理想,但是我尝试过在线搜索,但是我并没有真正走到任何地方,我并没有要求任何人完成它,只是为了指出正确的方向。

I've created this over the course of the last few hours, not much I know but so far so good. 我是在过去几个小时的过程中创建的,虽然我所知不多,但到目前为止还算不错。 Now I have to add to it so it prints two floats after the previous two results (The first sum and the modulo sum). 现在,我必须添加它,以便它在前两个结果(第一个和与模和)之后打印两个浮点数。 For example, it won't print 4 for 30/7, but rather 4.28 or whatever. 例如,对于30/7,它将不会打印4,而将显示4.28或其他值。 TIA :) TIA :)

    #include <stdio.h>
    int main()
    {
    int number1, number2, sum; //declares 3 variables
    printf("This is used to divide and find the modulo of two integers\n");
    printf("Enter your first integer: ");
    scanf("%d", &number1);
    printf("Enter your second integer: \n");
    scanf("%d", &number2);
    sum = number1 / number2; 
    printf("%d / %d = %d\n", number1, number2, sum);
    sum = number1 % number2; 
    printf("%d / %d = %\n", number1, number2, sum); 


return 0;

} }

To print a float use %f and convert the number to float : 要打印float使用%f并将数字转换为float

printf("%d / %d = %f\n", number1, number2, (float)number1 / number2);
//                 ^                       ^^^^^^^

Important things to note about this change (highlighted above): 有关此更改的重要注意事项(上面已突出显示):

  • Use %f for a float or a double %f用作floatdouble float
  • Cast one of the numbers to float or a double to make a floating point division. 将数字之一转换为float或将其转换为floatdouble float数。

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

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