Thread: Floats

  1. #1
    Registered User okel91's Avatar
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    8

    Floats

    Hi,
    Im new to programming and have found myself doing it because it is a requirement for my engineering course. I was wondering if there is anyway to limit the amount of decimal places displayed in the program when using a float or a double. Because I only need 3 decimal places shown not 6.

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Code:
    #include <stdio.h>
    
    int main(void)
    {
         float f = 1.23456789;
         printf("%.3f\n", f);
    
         return 0;
    }
    printf and scanf format codes

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    Are you expecting like this?
    Code:
    #include<stdio.h>
    main()
    {
            float a=12.343434;
            printf("%.3f",a);
    }

  4. #4
    Registered User okel91's Avatar
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    8
    Okay, got 3 places now. Thanks for the help.

  5. #5
    Registered User okel91's Avatar
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    8
    One other question. In my program I am asking for input of date in form of dd/mm/yy, what declaration would I need to make in order to store a value in that form.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    HI

    You can use string declaration
    char *ptr="dd/mm/yy"

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    44
    not a pointer since you want it to be variable so i would just say

    char date[10];

  8. #8
    Registered User okel91's Avatar
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    8
    Thanks, ill try it tonight

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A doubt about precision in Floats
    By avi2886 in forum C Programming
    Replies: 3
    Last Post: 11-27-2009, 03:05 PM
  2. variables larger than floats
    By a.mlw.walker in forum C Programming
    Replies: 13
    Last Post: 04-23-2009, 02:28 AM
  3. % and floats
    By confuted in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2003, 09:41 PM
  4. comparing two arrays of floats
    By COBOL2C++ in forum C++ Programming
    Replies: 7
    Last Post: 07-16-2003, 03:22 AM
  5. Using floats in conditional statements
    By DBB in forum C++ Programming
    Replies: 3
    Last Post: 09-12-2001, 07:54 AM