trunc() , truncf() , truncl() in C language
All three functions are used to remove digits after decimal point and return the modified decimal number. trunc() : Truncates a double value after the decimal point and gives the integer part as the result. The return value and the arguments are of the type double.
Syntax : double trunc(double x); Parameters: x :It takes a double value as an input and then truncates the values after the decimal point. Return Value : It returns a double value whose values after the decimal point is 0 only.
Time Complexity: O(1)
Space Complexity: O(1)
Examples:
Input : 3.5
Output : 3.0
Input : -3.8
Output : -3.0
C
#include <stdio.h>
#include <math.h>
int main()
{
double x1 = 2.0, x2 = 3.9, x3 = -3.3, x4 = 4.9;
printf ( " Truncated value is %lf \n" , trunc(x1) );
printf ( " Truncated value is %lf \n" , trunc(x2) );
printf ( " Truncated value is %lf \n" , trunc(x3) );
printf ( " Truncated value is %lf \n" , trunc(x4) );
return 0;
}
|
Output:
Truncated value is 2.000000
Truncated value is 3.000000
Truncated value is -3.000000
Truncated value is 4.000000
truncf() : It works same as trunc with the difference that it is for float instead of double. Takes a float value, removes digits after decimal point and return modified float.
Syntax : float truncf(float x); Parameters: x :It takes a floating point value as an input and then truncates the values after the decimal point. Return Value : It returns a float whose values after the decimal point is 0 only.
Time Complexity: O(1)
Space Complexity: O(1)
Examples:
Input : 4.5
Output : 4.0
Input : -2.8
Output : -2.0
C
#include <stdio.h>
#include <math.h>
int main()
{
float x1 = 2.0, x2 = 3.9, x3 = -3.3, x4 = 4.9;
printf ( " Truncated value is %f \n" , truncf(x1) );
printf ( " Truncated value is %f \n" , truncf(x2) );
printf ( " Truncated value is %f \n" , truncf(x3) );
printf ( " Truncated value is %f \n" , truncf(x4) );
return 0;
}
|
Output:
Truncated value is 2.000000
Truncated value is 3.000000
Truncated value is -3.000000
Truncated value is 4.000000
truncl() : This works similar for long double and functionality wise same as trunc() and truncf()
Syntax : long double truncl (long double x); Parameters: x :It takes a long double value as an input and then truncates the values after the decimal point. Return Value : It returns a decimal value whose values after the decimal point is 0 only.
Time Complexity: O(1)
Space Complexity: O(1)
Examples:
Input : 4351.5
Output : 4351.0
Input : -2008.8
Output : -2008.0
In case of positive values:
C
#include <stdio.h>
#include <math.h>
int main()
{
long double x1 = 2.0, x2 = 3.9, x3 = -3.3, x4 = 4.9;
printf ( " Truncated value is %Lf \n" , truncl(x1) );
printf ( " Truncated value is %Lf \n" , truncl(x2) );
printf ( " Truncated value is %Lf \n" , truncl(x3) );
printf ( " Truncated value is %Lf \n" , truncl(x4) );
return 0;
}
|
Output:
Truncated value is 2.000000
Truncated value is 3.000000
Truncated value is -3.000000
Truncated value is 4.000000
We can use trunc() function whenever we need to calculate the integer part from a double data type. The advantage of this function is that whatever the decimal value may be the integer part remains same. In the ceil or floor or round functions the integer value changes but in trunc function it does not.
Last Updated :
06 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...