double trunc(double x); float trunc(float x); long double trunc(long double x); double trunc(T x); //整數(shù)類型
trunc()函數(shù)采用單個參數(shù),并返回double,float或long double類型的值。此函數(shù)在<cmath>頭文件中定義。
trunc()函數(shù)采用單個參數(shù),其trunc值將被計算。
簡而言之,trunc()函數(shù)會截斷小數(shù)點后的值,并且僅返回整數(shù)部分。
#include <iostream> #include <cmath> using namespace std; int main() { double x = 10.25, result; result = trunc(x); cout << "trunc(" << x << ") = " << result << endl; x = -34.251; result = trunc(x); cout << "trunc(" << x << ") = " << result << endl; return 0; }
運行該程序時,輸出為:
trunc(10.25) = 10 trunc(-34.251) = -34
#include <iostream> #include <cmath> using namespace std; int main() { int x = 15; double result; result = trunc(x); cout << "trunc(" << x << ") = " << result << endl; return 0; }
運行該程序時,輸出為:
trunc(15) = 15
對于整數(shù)值,應(yīng)用trunc函數(shù)將返回相同的結(jié)果。所以它在實際中并不常用來表示整數(shù)值。