-
Notifications
You must be signed in to change notification settings - Fork 0
/
nicenumber
executable file
·59 lines (45 loc) · 886 Bytes
/
nicenumber
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
function nicenumber
{
integer=$(echo $1 | cut -d. -f1)
decimal=$(echo $1 | cut -d. -f2)
if [ "$decimal" != $1 ]
then
result="${DD:='.'}$decimal"
fi
thousands=$integer
while [ $thousands -gt 999 ]
do
remainder=$(($thousands%1000))
while [ ${#remainder} -lt 3 ]
do
remainder="0${remainder}"
done
result="${TD:=","}${remainder}${result}"
thousands=$(($thousands/1000))
done
nicenumber="$thousands$result"
if [ ! -z $2 ]
then
echo $nicenumber
fi
}
DD="."
TD=","
while getopts "d:t:" opt
do
case $opt in
d) DD="$OPTARG" ;;
t) TD="$OPTARG" ;;
esac
done
shift $(($OPTIND - 1))
if [ $# -eq 0 ]
then
echo "Usage: $(basename $0) [-d c] [-t c] number" >&2
echo "-t for the thousands delimiter"
echo "-d for the decimals delimiter"
exit 1
fi
echo "\$$(nicenumber $1 1) MXN"
exit 0