Jack
Jack

Reputation: 361

How to convert int to float in C?

I am trying to solve:

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

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

If the value of the number is 50 and the total is 100, I should get 50.00 as percentage and that is what I want. But I keep getting 0.00 as the answer and tried many changes to the types but they didn't work.

Upvotes: 30

Views: 519137

Answers (11)

Hypothalamus
Hypothalamus

Reputation: 1

Try adding 0.00 to the integer, it would instantly turn it into a float.

Upvotes: 0

user3552314
user3552314

Reputation: 1

The order matters. You can't just put a double somewhere in the expression. The parser has to come across it before it does any integer arithmetic.

int obs, tot;
float percent;
obs = 17;
tot = 40;
printf("%f\n",
        percent = obs / tot * 100);
printf("%f\n",
        percent = obs / tot * 100.0);
printf("%f\n",
        percent = 100.0 * obs / tot);
printf("%f\n",
        percent = obs*100.0/ tot);
printf("%f\n",
        percent = 100.0* (obs / tot) );

Gives output:

0.000000
0.000000
42.500000
42.500000
0.000000

So the double has to appear in the first operation to be performed. The third and fourth methods work OK but note that parentheses can prioritise the integer division, so fifth fails.

Upvotes: 0

#include <stdio.h>
#include <conio.h>

int main() {

    int i,ortust=0,ortalt=0,toplam,dizi[5],sayac;
    float ortalama;
    
    for(i=0;i<5;i++)
    {
        printf("%d. sayiyi giriniz ",i++);
        scanf("%d",&dizi[i]);
        sayac++;
        toplam+=dizi[i];
    }
    
    ortalama=(float)(toplam/sayac);
    for(i=0;i<5;i++){
        if(dizi[i]<ortalama){
            ortalt++;
        }
        else if (dizi[i]>ortalama){
            ortust++;
        }
    }
    printf("Ortalama: %2.f\n",ortalama);
    printf("Ortalamanin altinda kalan eleman sayisi: \n ",ortalt);
    printf("Ortalamanin ustunde kalan eleman sayisi: ",ortust); 
    
    
    

    getch();
    return 0;
}

Upvotes: 0

mdsiaofficial
mdsiaofficial

Reputation: 80

Just Make the number variable int to float.

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

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

Add (float) before the variable name when you use.

Upvotes: 0

Arumugam Maharaja
Arumugam Maharaja

Reputation: 1

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;
}

Upvotes: 0

Alan Corey
Alan Corey

Reputation: 611

I routinely multiply by 1.0 if I want floating point, it's easier than remembering the rules.

Upvotes: 3

Daniel Fischer
Daniel Fischer

Reputation: 183988

Integer division truncates, so (50/100) results in 0. You can cast to float (better double) or multiply with 100.0 (for double precision, 100.0f for float precision) first,

double percentage;
// ...
percentage = 100.0*number/total;
// percentage = (double)number/total * 100;

or

float percentage;
// ...
percentage = (float)number/total * 100;
// percentage = 100.0f*number/total;

Since floating point arithmetic is not associative, the results of 100.0*number/total and (double)number/total * 100 may be slightly different (the same holds for float), but it's extremely unlikely to influence the first two places after the decimal point, so it probably doesn't matter which way you choose.

Upvotes: 40

Lundin
Lundin

Reputation: 215360

Change your code to:

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

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

Upvotes: 0

ALOToverflow
ALOToverflow

Reputation: 2699

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

Upvotes: 0

Omkant
Omkant

Reputation: 9234

integer division in C truncates the result so 50/100 will give you 0

If you want to get the desired result try this :

((float)number/total)*100

or

50.0/100

Upvotes: 11

Some programmer dude
Some programmer dude

Reputation: 409482

No, because you do the expression using integers, so you divide the integer 50 by the integer 100, which results in the integer 0. Type cast one of them to a float and it should work.

Upvotes: 2

Related Questions