No bash
, no external utils, just pure (ugly) POSIX shell code in two functions:
rhup ()
{
[ "${1##*.[5-9]*}" ]
echo "$((${1%%.*}+$?))"
}
req ()
{
a=
[ "$(rhup "$1")" = "$(rhup "$2")" ] || a="not "
echo "When rounded half up $1 and $2 are ${a}equal."
}
Demo:
req 2 3 ; req 2 2.2 ; req 2.4 2.5
Output:
When rounded half up 2 and 3 are not equal.
When rounded half up 2 and 2.2 are equal.
When rounded half up 2.4 and 2.5 are not equal.
How it works:
Given a number rhup
(short for round half up) uses shell parameter substitution to check if a half-up decimal suffix exists. Then it adds the resulting error code of 0
or 1
to the number's integer prefix and prints the sum.
req
(short for rhup equal) runs rhup
on two numbers, compares them, if they're not equal, sets $a
to "not ", then prints the desired conditional English sentence.
Neither function does any error checking on the input values.