forked from kneutron/ansitest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommasep-num.mrg
More file actions
25 lines (19 loc) · 821 Bytes
/
commasep-num.mrg
File metadata and controls
25 lines (19 loc) · 821 Bytes
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
#!/bin/bash
# Returns a passed number as comma-separated e.g. pass $1 = 80000000000 and receive 80,000,000,000
# REF: https://unix.stackexchange.com/questions/113795/add-thousands-separator-in-a-number
function commanum () {
LC_NUMERIC=en_US printf "%'.f" $1
}
# test for interactive shell / OK to echo text
#[ $(echo $- |grep i |wc -l) -gt 0 ] &&
[ ! "$1" = "" ] && commanum $1
exit;
# Example usage:
#$ tmpvar=$(./commasep-num.mrg 80000000000)
#$ echo $tmpvar
#80,000,000,000
# Limitation: it doesn't handle fractional numbers x.xx , just integers - anything after the decimal gets dropped
# so don't try to display π (pi) with it ;-)
# this appears to work somewhat with fractionals but is not bash-internal:
#$ echo 1232323.1415927 | awk '{printf(fmt,$1)}' fmt="%'19.19f\n"
#1,232,323.1415927000343799591