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.

Examples:

Input : 3.5
Output : 3.0

Input : -3.8
Output : -3.0
filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to demonstrate 
// trunc() function
#include <stdio.h>
  
// library containing trunc function
#include <math.h>     
  
// Driver function
int main()
{
    // using trunc function which return
    // Truncated value of the input 
    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) );
  
    // For negative values
    printf(" Truncated value is %lf \n", trunc(x3) );
    printf(" Truncated value is %lf \n", trunc(x4) ); 
    return 0;
}

chevron_right


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.

Examples:

Input : 4.5
Output : 4.0

Input : -2.8
Output : -2.0
filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to demonstrate truncf() function
#include <stdio.h>
#include <math.h>     
  
// Driver function
int main()
{
    float x1 = 2.0, x2 = 3.9, x3 = -3.3, x4 = 4.9;
  
    // using truncf function which return
    // Truncated value of the input 
    printf(" Truncated value is %f \n", truncf(x1) );
    printf(" Truncated value is %f \n", truncf(x2) );
  
    // For negative values
    printf(" Truncated value is %f \n", truncf(x3) );
    printf(" Truncated value is %f \n", truncf(x4) ); 
    return 0;
}

chevron_right


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.

Examples:

Input : 4351.5
Output : 4351.0

Input : -2008.8
Output : -2008.0

In case of positive values:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to demonstrate truncl() function
#include <stdio.h>
#include <math.h>     
  
// Driver function
int main()
{
    long double x1 = 2.0, x2 = 3.9, x3 = -3.3, x4 = 4.9;
  
    // using truncf function which return
    // Truncated value of the input 
    printf(" Truncated value is %Lf \n", truncl(x1) );
    printf(" Truncated value is %Lf \n", truncl(x2) );
  
    // For negative values
    printf(" Truncated value is %Lf \n", truncl(x3) );
    printf(" Truncated value is %Lf \n", truncl(x4) ); 
    return 0;
}

chevron_right


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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.