Tuesday, September 25, 2012

Number formatting with thousand separator

in BASH : (Ref: http://stackoverflow.com/questions/9374868/number-formatting-in-bash-with-thousand-separator)

$ printf "%'.3f\n" 12345678.901
12,345,678.901
$ printf "%'d\n" 12345678
12,345,678

in AWK: (Ref: http://www.mkssoftware.com/docs/man1/awk.1.asp)

When flag contains a ' or " character and the TK_USE_CURRENT_LOCALE environment variable is set, a thousands separator is displayed. The digital grouping character (for example, a comma in the United States) as set by the Regional and Language Options control panel applet is used as the thousands separator. When flags contains ' or " and TK_USE_CURRENT_LOCALE is unset, no thousands separator is displayed.
For example, the following MKS KornShell commands:
export TK_USE_CURRENT_LOCALE=1
awk 'BEGIN { printf("%'\''10d\n",123456)}'
display:
123,456
while the MKS KornShell commands:
unset TK_USE_CURRENT_LOCALE
awk 'BEGIN { printf("%'\''10d\n",123456)}'
display:
123456

No comments:

Post a Comment