Fish Shell:
Rounding numbers

How to:

In Fish, rounding numbers hinges on the math command. Use math -s0 to round to the nearest integer.

# Round up
echo (math -s0 "4.7")
# Output: 5

# Round down
echo (math -s0 "4.3")
# Output: 4

# Round to two decimal places
echo (math -s2 "4.5678")
# Output: 4.57

# Round negative number
echo (math -s0 "-2.5")
# Output: -3

Deep Dive

Historically, rounding numbers was done more manually or with external tools, but in modern shells like Fish, it’s baked into built-in utilities. Fish’s approach using the math command simplifies things compared to older shells. Alternatives in other programming environments vary; languages like Python use functions like round(), while Bash might require more complex expressions or bc utility. Fish’s rounding implementation simplifies scripting by keeping the math inside the shell environment instead of invoking other tools or languages.

See Also