Float point number

This is a discussion on Float point number within the C Programming forums, part of the General Programming Boards category; Hi I was wondering if anyone knew of any good resources that could point me in the right direction for ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    Float point number

    Hi I was wondering if anyone knew of any good resources that could point me in the right direction for learning about float points. Basically all I want to do is divide two numbers like 5/2 and be able to print it with something other than just 2.
    BTW. I'm using code blocks, not that that really means anything.

    Any help would be greatly appreciated....

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    Do you mean type casting? You can also declare a variable like result as a float and then if you divide 5 by 2 it will come out as a float.

    ex.
    float answer;
    answer=(float)5/2;
    printf("%.2f\n", answer);
    Last edited by camel-man; 02-09-2012 at 07:52 PM.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    2
    thanks Camel-man for the help, please take a look at a sample of what i have
    Here's basically what I would like to accomplish. When I run this code, it just comes back with a zero....hmmmm
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int num1;
        int num2;
        float div;
    
    
        printf("What number to divide :\n");
            scanf("%d", &num1);
        printf("By : \n");
            scanf("%d", &num2);
    
    
        div = (float) num1 / num2;
    
    
        printf("The answer is %d\n", div);
    
    
    return 0;
    }

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    In your final printf statment you are displaying it as a regular integer example is %d, what you need is %f for float. If you want to display only up to two decimals you can say %.2f instead of %f.
    Last edited by camel-man; 02-09-2012 at 08:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiply two float number
    By hqt in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2011, 03:35 AM
  2. float number division
    By hoistyler in forum C Programming
    Replies: 6
    Last Post: 01-14-2009, 03:13 AM
  3. Triming a float number
    By RocketMan in forum C Programming
    Replies: 3
    Last Post: 10-01-2008, 09:31 AM
  4. Floating point number
    By Mckooy in forum C Programming
    Replies: 1
    Last Post: 01-15-2006, 07:28 AM
  5. Float number and sendmessage
    By chris1985 in forum C Programming
    Replies: 1
    Last Post: 06-07-2005, 10:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21