how to convert int to float

Hi all,

I have two variable, count and totalcount, both are integer

My result is declared as float,

When I perform

result = count/totalcount;

it did not return a float as it should be.

Instead, it return 0 (eg 5/25)

How can I convert it to float

Thanks



Answer this question

how to convert int to float

  • mobigital

    Sorry Keyu, but you posted the same wrong answer as was previously posted...

  • cdun2

    int count, totalCount;

    count = 5;

    totalCount = 25;

    float result = Convert.ToSingle(totalCount /count );

    I hope this will help.

    Best Regards,

    Rizwan aka RizwanSharp



  • Gianni Marzaloni

    Matthew Watson wrote:
    That won't work Rizawan...

    totalCount/count will still be calculated in integer precision.

    The correct answer is:

    float result = count / (float)totalCount;

    Sorry Matthew,

    You are right I just took the quesiton wrong as you can assume from my answer above.

    Best Regards,

    Rizwan aka RizwanSharp



  • Ro0ke

    int a = 4;
    int b = 25;
    float result = ((float)(a))/b;

    result is 0.16

    Grtz
    Adriaan

  • DalekDAW

    Hi,

    Thanks for your solution.


  • Penicillin

    That won't work Rizawan...

    totalCount/count will still be calculated in integer precision.

    The correct answer is:

    float result = count / (float)totalCount;


  • Criminet

    Insead of type conversion of Variables u can simly use function

    Convert.ToFloat(a/b);



  • how to convert int to float