c语言trunc函数
C ++ trunc()函数 (C++ trunc() function)trunc() function is a library function of cmath header, it is used to round (truncate) the value toward zero, it accepts a number and returns the nearest integral value that is not the larger in magnitude than the given number.
trunc()函数是cmath标头的库函数,用于将值四舍五入(截断)为零,它接受一个数字并返回其大小不大于给定数字的最近整数值。
Syntax of trunc() function:
trunc()函数的语法:
trunc(x);Parameter(s): x – is the number to round toward zero.
参数: x –是要舍入为零的数字。
Return value: double – it returns double type value that is the rounded (truncated) value of the number x.
返回值: double-返回double类型的值,该值是数字x的舍入(截断)值。
Example:
例:
Input: float x = 2.3; Function call: trunc(x); Output: 2 Input: float x = 2.8; Function call: trunc(x); Output: 2 C ++代码演示trunc()函数的示例 (C++ code to demonstrate the example of trunc() function) // C++ code to demonstrate the example of // trunc() function#include <iostream>#include <cmath>using namespace std;// main() sectionint main(){ float x; //input the number cout<<"Enter a float value: "; cin>>x; //rounding doward zero cout<<"trunc("<<x<<"): "<<trunc(x); cout<<endl; return 0;}Output
输出量
First run:Enter a float value: 2.3 trunc(2.3): 2 Second run:Enter a float value: 2.5 trunc(2.5): 2Third run:Enter a float value: 2.8 trunc(2.8): 2 Fourth run:Enter a float value: -2.3trunc(-2.3): -2 Fifth run:Enter a float value: -2.5trunc(-2.5): -2Sixth run:Enter a float value: -2.8trunc(-2.8): -2翻译自: https://www.includehelp.com/cpp-tutorial/trunc-function-with-example.aspx
c语言trunc函数