float value

This is a discussion on float value within the C Programming forums, part of the General Programming Boards category; hi all Code: #include <stdio.h> main() { float i,j=100.0,k=19456.0; i=j/k; printf(******** %f \n",i); } output is ******** 0.005139803 i would ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    float value

    hi all
    Code:
    #include <stdio.h>
    main()
    {
    float i,j=100.0,k=19456.0;
    i=j/k;
    printf(******** %f \n",i);
    }
    output is
    ******** 0.005139803

    i would like round figure it like
    0.005.

    how can i do this

    greetings munna

  2. #2
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    You can control the precision by trying something like : "&#37;.af" where 'a' is the number of digits following the decimal point.
    In the middle of difficulty, lies opportunity

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    also you should learn to use int main()
    and properly indent your code
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    Looking at your code, I am guessing you must be using a compiler like TC or BC. Please switch over th GCC. It will help you in the long run.
    In the middle of difficulty, lies opportunity

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    136
    Quote Originally Posted by kris.c View Post
    You can control the precision by trying something like : "%.af" where 'a' is the number of digits following the decimal point.
    sorry. it is not working..

  6. #6
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    Ofcourse it does,
    Code:
     # include<stdio.h>
    int main()
    {
            float i,j=100.0,k=19456.0;
            i=j/k;
            printf("******** &#37;.3f \n",i);
            printf("******** %.5f \n",i);
    return 0;
    }

    my output:
    Code:
    ******** 0.005
    ******** 0.00514
    In the middle of difficulty, lies opportunity

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by kris.c View Post
    Looking at your code, I am guessing you must be using a compiler like TC or BC. Please switch over th GCC. It will help you in the long run.
    What the heck leads you to guess that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 04:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

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