usr/lib/byobu/.shutil: fix rounding across a decimal point in fpdiv()

This commit is contained in:
Scott Moser 2011-05-25 14:58:52 -04:00
commit 6468728e86
2 changed files with 18 additions and 9 deletions

4
debian/changelog vendored
View file

@ -1,8 +1,12 @@
byobu (4.3) unreleased; urgency=low byobu (4.3) unreleased; urgency=low
[ Dustin Kirkland ]
* usr/lib/byobu/uptime: fix string bug in uptime introduced by smoser's * usr/lib/byobu/uptime: fix string bug in uptime introduced by smoser's
changes changes
[ Scott Moser ]
* usr/lib/byobu/.shutil: fix rounding across a decimal point in fpdiv()
-- Dustin Kirkland <kirkland@ubuntu.com> Mon, 23 May 2011 19:21:36 -0500 -- Dustin Kirkland <kirkland@ubuntu.com> Mon, 23 May 2011 19:21:36 -0500
byobu (4.2-0ubuntu1) oneiric; urgency=low byobu (4.2-0ubuntu1) oneiric; urgency=low

View file

@ -41,18 +41,23 @@ find_script() {
# third argument indicates how many digits after the decimal # third argument indicates how many digits after the decimal
fpdiv() { fpdiv() {
local a=$1 b=$2 pres=${3:-3} local a=$1 b=$2 pres=${3:-3}
local i=0 mp="10" whole="" part="" local i=0 mp="10" whole="" part="" chunk="" n=0
while i=$(($i+1)) && [ $i -le $pres ]; do while i=$(($i+1)) && [ $i -le $pres ]; do
mp="${mp}0" mp="${mp}0"
chunk="${chunk}?"
done done
whole=$(($a/$b))
part=$((((($a%$b)*${mp})/$b))) n=$(((${mp}*${a})/${b}))
if [ $part -eq 0 ]; then
part=${part#1}; # round up if necessary
elif [ $((${part}%(${mp}/10))) -ge 5 ]; then [ $(($n%${mp})) -ge $((${mp}/2)) ] && n=$(($n+5))
part=$(($part+5))
fi # drop the last digit, which was only there for rounding
_RET="${whole}.${part%?}" n=${n%?}
whole=${n%${chunk}}
part=${n#${whole}}
_RET=${whole}${part:+.${part}}
return
} }
# rtrim(string,chars) # rtrim(string,chars)