locked
Convert variable to percentage RRS feed

  • Question

  • Hiya Guys!

    I have a variable. I then try and put the percentage (so it is 4 out of 10, equating to 40%) into a database via a query. I tried...

     

    int x=4;
    
    double percentage = x/10 * 100;

     

    ...but it still puts 4 into the database. Any ideas why?

    Thanks for your help!

     

    Monday, January 9, 2012 8:38 PM

Answers

  • This will produce 0 because the integer value of 4/10 is 0.

    You will have to cast x to double like this:

    double percentage = (double)x/10 * 100
    
    
    

    or you can declare your x as double.

    hope this helps,

    sivilian

     

    • Marked as answer by matty1491 Monday, January 9, 2012 8:51 PM
    Monday, January 9, 2012 8:45 PM