How to convert int to float in C?
Asked Answered
K

11

29

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.

Ken answered 23/11, 2012 at 13:34 Comment(1)
Because the int part of 50/100 (= 0.5) is 0Haleigh
D
40

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.

Dumbbell answered 23/11, 2012 at 13:40 Comment(1)
The former performs the calculation with double precision, the latter with single precision. So they may not be equivalent. Use 100.0f to get a float literal.Spinous
O
11

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
Okelley answered 23/11, 2012 at 13:40 Comment(0)
H
3

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

Haslam answered 13/6, 2018 at 1:57 Comment(0)
C
2

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.

Civies answered 23/11, 2012 at 13:39 Comment(0)
A
2

You are doing integer arithmetic, so there the result is correct. Try

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

BTW the %f expects a double not a float. By pure luck that is converted here, so it works out well. But generally you'd mostly use double as floating point type in C nowadays.

Andrel answered 23/11, 2012 at 13:39 Comment(0)
V
0

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

Visitant answered 23/11, 2012 at 13:41 Comment(0)
S
0

Change your code to:

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

percentage=((float)number/total)*100f;
printf("%.2f", (double)percentage);
Spinous answered 23/11, 2012 at 14:21 Comment(0)
B
0

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;
}
Benefit answered 16/12, 2018 at 3:1 Comment(0)
F
0

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.

Foxglove answered 6/7, 2022 at 2:27 Comment(0)
A
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;
}
Athalla answered 2/11, 2022 at 22:31 Comment(0)
S
0

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.

Subjectify answered 10/2, 2023 at 17:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.