C 語言的 trunc()、truncf()、truncl()


我們將介紹三個函式。這些函式是 trunc()、truncf() 和 truncl()。這些函式用於將浮點值轉換成截斷形式。

trunc() 函式

此函式用於截斷 double 型別的值,並只返回整數部分。其語法如下。

double trunc(double argument)

示例

#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); }

輸出

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

truncf() 函式

此函式用於截斷 floating 型別的值,並只返回整數部分。其語法如下。

float tuncf(float argument)

示例

#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); }

輸出

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

truncl() 函式

此函式類似於 trunc() 或 truncf(),但主要差別在於,此函式用於截斷 long double 型別的值,並只返回整數部分。

其語法如下。

long double truncl(long double argument)

示例

#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); }

輸出

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

更新於: 2019 年 07 月 30 日

2K+ 瀏覽量

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.