How to divide integers and get a float in C

前端 未结 2 627
余生分开走
余生分开走 2021-01-27 13:11

I\'m building a paragraph difficulty labeler by getting the number of words, sentences and letters.... I\'m trying to divide to integers and get a float for example ((let

相关标签:
2条回答
  • 2021-01-27 13:27

    You have to cast one of the ints to a float. For example:

    float L = (((float)letter_counter / word_counter) * 100);
    
    0 讨论(0)
  • 2021-01-27 13:35

    You could put 100.00 rather than 100 to tell the compiler to evaluate the expression as a floating point expression as follows:

    int num1 = 80;
    int num2 = 21;
    float L = (num1 * 100.00) / num2; // 100 -> 100.00
    

    It'll result:

    380.952393
    
    0 讨论(0)
提交回复
热议问题