def round_whole:
# Basic round function, returns the closest whole number
# Usage:
# 2.6 | round_whole // 3
. + 0.5 | floor
;
def round(num_dec):
# Round function, takes num_dec as argument
# Usage: 2.2362 | round(2) // 2.24
num_dec as $num_dec |
# First multiply the number by the number of decimal places we want to round to
# i.e 2.2362 becomes 223.62
. * pow(10; $num_dec) |
# Then use the round_whole function
# 223.62 becomes 224
round_whole |
# Then divide by the number of decimal places we want to round by
# 224 becomes 2.24 as expected
. / pow(10; $num_dec)
;
jq --null-input --raw-output \
'
def round_whole:
# Basic round function, returns the closest whole number
# Usage:
# 2.6 | round_whole // 3
. + 0.5 | floor
;
def round(num_dec):
# Round function, takes num_dec as argument
# Usage: 2.2362 | round(2) // 2.24
num_dec as $num_dec |
# First multiply the number by the number of decimal places we want to round to
# i.e 2.2362 becomes 223.62
. * pow(10; $num_dec) |
# Then use the round_whole function
# 223.62 becomes 224
round_whole |
# Then divide by the number of decimal places we want to round by
# 224 becomes 2.24 as expected
. / pow(10; $num_dec)
;
[
2.2362,
2.4642,
10.23423
] |
map(
round(2)
)
'
2条答案
按热度按时间9w11ddsr1#
似乎
round
在您的版本中不可用。请升级jq或使用floor
实现round
:用法示例:
mznpcxlj2#
我们可以使用pow函数和
. + 0.5 | floor
来创建我们自己的"round"函数,该函数以要舍入的值作为输入,以小数位数作为参数。产量