在 Julia 中获取 x 的最接近整数值 – trunc() 方法
trunc()
是 julia 中的内置函数,用于返回与绝对值小于或等于x的指定值x相同类型的最接近的整数值。
Syntax: trunc(x)
Parameters:
- x: Specified value.
Returns: It returns the nearest integral value of the same type as the specified value x whose absolute value is less than or equal to x.
示例 1:
# Julia program to illustrate
# the use of trunc() method
# Getting the nearest integral value
# of the same type as the specified value
# x whose absolute value is less
# than or equal to x
println(trunc(3))
println(trunc(0.5))
println(trunc(3.9))
输出:
3
0.0
3.0
示例 2:
# Julia program to illustrate
# the use of trunc() method
# Getting the nearest integral value
# of the same type as the specified value
# x whose absolute value is less
# than or equal to x
println(trunc(0))
println(trunc(2))
println(trunc(2.1))
println(trunc(2.5))
println(trunc(2.9))
输出:
0
2
2.0
2.0
2.0