How can I round, floor, ceil, and truncate a number in jq jq-1.5-1-a5b5cbe?
For example, with {"mass": 188.72}
, I want {"mass": 188}
with floor, {"mass": 189}
with ceil and round.
Rounding examples:
5.52 --> 6
5.50 --> 5 or 6
-5.52 --> -6
Truncate examples:
5.52 --> 5
5.50 --> 5
-5.52 --> -5
I have come up with -5 as $n | if $n > 0 then [range($n+0.00000000000001)] else [range(-$n)] end | last
for truncate, but it is needlessly complex (and likely contains bugs).
Some builds may lack those functions, but as far as I'm concerned floor
is widely available; so, you can implement them using it.
def round: . + 0.5 | floor;
def ceil: if . | floor == . then . else . + 1.0 | floor end;
def trunc: if . < 0 then ceil else floor end;