Re: Remove x number of decimals from a float value

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Fabio Miranda Hamburger wrote:
Hello,

I am trying to implement a 100% math function ( avoid handle the float as a char array), that remove x number of decimals from the right to the float point.

Example:
Given x= 12345.6789 I call the function with (x,2) and the returned number is: x= 12345.67, for (x,4) returned value is x=12345

I was trying to implement this function using datatype casting, abs() and other functions from math.h library.

How about :


double f( double x, int n )
{
        return ( n>0 ? f( x*10,n-1 )/10 : floor(x) );
}

Oops, 'n' is number of decimals you want to keep, not throw away.


/Per Jessen, Zurich
-
: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux