How to convert int to float in C?

后端 未结 9 727
花落未央
花落未央 2020-12-03 07:38

I am trying to solve:

int total=0, number=0;
float percentage=0.0;

percentage=(number/total)*100;
printf(\"%.2f\", percentage);

If the val

相关标签:
9条回答
  • 2020-12-03 08:11

    This should give you the result you want.

    double total = 0;
    int number = 0;
    float percentage = number / total * 100
    printf("%.2f",percentage);
    

    Note that the first operand is a double

    0 讨论(0)
  • 2020-12-03 08:11

    This can give you the correct Answer

    #include <stdio.h>
    int main()
    {
        float total=100, number=50;
        float percentage;
        percentage=(number/total)*100;
        printf("%0.2f",percentage);
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-03 08:18

    This Should work Making it Round to 2 Point

           int a=53214
           parseFloat(Math.round(a* 100) / 100).toFixed(2);
    
    0 讨论(0)
提交回复
热议问题