Thread: need to convert double to number with 1 digit after the floating point

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    3

    need to convert double to number with 1 digit after the floating point

    I'm getting double as a result of a function.

    now I have to convert it to be a number with only one digit after the floating point, how can I do it?

    is there a math function that can help me?

    thanx

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    3

    only one Digit after the floating point

    double d = 1234.567;
    printf( "%.1f\n", d );


    the printf only shows the number as if it was with one digit after the floating point.

    BUT, I need to save it as a new double/float or even override the double I get from the function, this new variable should have only one digit after the floating point - all the other digits must be zero!!!

    I'm using it for a rotation function in OpenGL

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    3

    Can I get the number I'm looking for?

    I looked at http://www.eskimo.com/~scs/C-faq/q14.1.html
    and it is about the printing method again.

    are you trying to tell me that there is no way I can get a number with only one digit after the floating point?

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    It's pure maths:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    	double a = 5.123456, b;
    
    	b = floor( a * 10 ) / 10;
    
    	printf( "%lf\n", b );
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For the numerical recipes in C types!
    By Smattacus in forum C Programming
    Replies: 5
    Last Post: 10-28-2008, 07:57 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  4. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM