double trunc(double x); float trunc(float x); long double trunc(long double x); double trunc(T x); //整數(shù)類型
trunc()函數(shù)采用單個(gè)參數(shù),并返回double,float或long double類型的值。此函數(shù)在<cmath>頭文件中定義。
trunc()函數(shù)采用單個(gè)參數(shù),其trunc值將被計(jì)算。
簡(jiǎn)而言之,trunc()函數(shù)會(huì)截?cái)嘈?shù)點(diǎn)后的值,并且僅返回整數(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;
}運(yùn)行該程序時(shí),輸出為:
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;
}運(yùn)行該程序時(shí),輸出為:
trunc(15) = 15
對(duì)于整數(shù)值,應(yīng)用trunc函數(shù)將返回相同的結(jié)果。所以它在實(shí)際中并不常用來表示整數(shù)值。