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
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP