如何在C中除整数并获得浮点数 [英] How to divide integers and get a float in C

查看:114
本文介绍了如何在C中除整数并获得浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过获取单词,句子和字母的数量来构建段落难度标签....我试图将其除以整数并获得浮点数,例如((letter_counter/word_counter)* 100); 除法(80/21)* 100返回400,而不是380.9523,我在做什么错?感谢一些帮助!这是我的代码示例!字母计数器,单词计数器,句子计数器=整数

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 ((letter_counter / word_counter) * 100); Dividing (80 / 21) * 100 is returning 400 instead of 380.9523, What am I doing wrong? Appreciate some help! Here is an example of my code! letter_counter, word_counter, sentence_counter = int


    // Putting all together
    float L = ((letter_counter / word_counter) * 100);
    printf("The L value = %f\n", L);

    float S = ((float) sentence_counter / word_counter) * 100;
    printf("The S value = %f\n", S);

    int index = round(0.0588 * L - 0.296 * S - 15.8);
    printf("Grade %i\n", index);

    if (index < 1)
    {
        printf("Before Grade 1\n");
    }
    else if (index >= 16)
    {
        printf("Grade 16+\n");
    }
    else
    {
        printf("Grade %i\n", index);
    }

推荐答案

您可以输入 100.00 而不是 100 来告诉编译器将表达式评估为浮点数表达式如下:

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

它将得到:

380.952393

这篇关于如何在C中除整数并获得浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆