ที่นี่เราจะเห็นสามฟังก์ชั่น ฟังก์ชันเหล่านี้คือ trunc(), truncf() และ truncl() ฟังก์ชันเหล่านี้ใช้เพื่อแปลงค่าทศนิยมให้อยู่ในรูปแบบที่ถูกตัดทอน
ฟังก์ชัน trunc()
ฟังก์ชันนี้ใช้เพื่อตัดทอนค่าประเภทคู่ และส่งคืนเฉพาะส่วนจำนวนเต็ม ไวยากรณ์เป็นเหมือนด้านล่าง
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\n",a); printf("The value of a: %lf\n",b); }
ผลลัพธ์
The value of a: 53.000000 The value of a: 75.000000
ฟังก์ชัน truncf()
ฟังก์ชันนี้ใช้เพื่อตัดทอนค่าประเภทลอยตัว และส่งคืนเฉพาะส่วนจำนวนเต็ม ไวยากรณ์เป็นเหมือนด้านล่าง
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\n",a); printf("The value of a: %f\n",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\n",a); printf("The value of a: %Lf\n",b); }
ผลลัพธ์
The value of a: 53547.000000 The value of a: 78547.000000