Thread: Round conversion to 2 decimal places

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    36

    Round conversion to 2 decimal places

    I'm attempting to convert feet into metres by multiplying the feet (int data type) by 0.3048 since 1 foot is 0.3048 in metres.

    After doing so in my function that returns a double data type, I'm attempting to print the new value using the printf command and that command is like so:

    Code:
    altMetres = convert_feet_to_metres(altitude);
    
    printf("\n\tTime: %ds, Altitude: %.2dm, Position: %f'N %f'E", timeStamp, altMetres, decDegreeLat, decDegreeLon);
    My Conversion function:

    Code:
    double convert_feet_to_metres(int feet)
    {
    	/* one foot is 0.3048 in metres */
    	return (feet * 0.3048);
    }
    When the value stored in altitude is 3566, the above code prints the following after my function is invoked, something weird is happening, why is Altitude -845249564 and not 1086.9168. I'm using the %.2d (format specifier) in a poor attempt to get the value (when correct calculation is done) to output 1086.92 since I need two decimal places, I ain't got this far since the calculation is all wrong.

    Time: 1106059583s, Altitude: -845249564m, Position: 0.000000'N -0.000000'E
    If anyone can give us a hint, would be most appreciated.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    probably your format is not suitable to the variable type - check it or show the var definitions
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    fixed it, I changed %.2dm to %.2fm. I throught the d (format specifier) was for when specifying a double since the variable the conversion is stored in is a double data type. %f being for floats.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Cero.Uno View Post
    fixed it, I changed %.2dm to %.2fm. I throught the d (format specifier) was for when specifying a double since the variable the conversion is stored in is a double data type. %f being for floats.
    Such things should not be guessed - read the manual for printf/scanf formats. And note that they are not fully equivalent
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Cero.Uno View Post
    fixed it, I changed %.2dm to %.2fm. I throught the d (format specifier) was for when specifying a double since the variable the conversion is stored in is a double data type. %f being for floats.
    Floats are implicitly cast to double, when passed to printf, so they have the same format specifier. Much like chars and shorts are cast to int.

    You really should check a reference for these kinds of things.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal places
    By Gordon in forum Windows Programming
    Replies: 9
    Last Post: 06-08-2008, 09:04 AM
  2. Decimal places
    By Gordon in forum Windows Programming
    Replies: 4
    Last Post: 09-28-2007, 10:03 AM
  3. Is there a way to round to 2 decimal places?
    By osiris^ in forum C Programming
    Replies: 10
    Last Post: 07-27-2007, 02:08 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM