首页 > 文章列表 > 在C语言中,trunc()表示截断函数,truncf()表示截断函数(单精度),truncl()表示截断函数(长双精度)

在C语言中,trunc()表示截断函数,truncf()表示截断函数(单精度),truncl()表示截断函数(长双精度)

c语言 截断函数 trunc truncf truncl
496 2023-08-20

Here we will see three functions. These functions are trunc(), truncf() and the truncl(). These functions are used to convert floating point values into truncated form.

The trunc() Function

This function is used to truncate double type value. And return only the integer part. The syntax is like below.

double trunc(double argument)

Example

#include <stdio.h>
#include <math.h>
main() {
   double a, b, x, y;
   x = 53.26;
   y = 75.86;
   a = trunc(x);
   b = trunc(y);
   printf("The value of a: %lf

",a);    printf("The value of a: %lf

",b); }

Output

The value of a: 53.000000
The value of a: 75.000000

truncf()函数

此函数用于截断浮点类型的值,并仅返回整数部分。语法如下所示。

float tuncf(float argument)

Example

的中文翻译为:

示例

#include <stdio.h>
#include <math.h>
main() {
   float a, b, x, y;
   x = 53.26;
   y = 75.86;
   a = truncf(x);
   b = truncf(y);
   printf("The value of a: %f

",a);    printf("The value of a: %f

",b); }

Output

The value of a: 53.000000
The value of a: 75.000000

The truncl() Function

This is like trunc() or truncf(). But the main difference is, this function is used to truncate long double type value. And return only the integer part.

The syntax is like below.

long double truncl(long double argument)

Example

#include <stdio.h>
#include <math.h>
main() {
   long double a, b, x, y;
   x = 53547.55555555555;
   y = 78547.55555555523;
   a = truncl(x);
   b = truncl(y);
   printf("The value of a: %Lf

",a);    printf("The value of a: %Lf

",b); }

Output

The value of a: 53547.000000
The value of a: 78547.000000