|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Format Money/Currency (U.S.)
I have looked everywhere.
Does bash have a money/currency format for output? Do I have to use awk or printf? Thank you |
| Sponsored Links | ||
|
|
|
#2
|
|||
|
|||
|
You can use both: Code:
# var=52.5259
# printf "$%.2f\n" $var
$52.53
# echo $var | awk '{printf "$%.2f\n",$1}'
$52.53all depends on context. |
|
#3
|
||||
|
||||
|
From sed one liners Code:
# add commas to numeric strings, changing "1234567" to "1,234,567"
gsed ':a;s/\B[0-9]\{3\}\>/,&/;ta' # GNU sed
sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' # other seds
# add commas to numbers with decimal points and minus signs (GNU sed)
gsed -r ':a;s/(^|[^0-9.])([0-9]+)([0-9]{3})/\1\2,\3/g;ta'http://sed.sourceforge.net/sed1line.txt |
|
#4
|
||||
|
||||
|
Another way to print the thousands separator is to use the printf ' format flag. For example Code:
$ var=123456.78 $ printf "$%'.2f\n" $var $ $123,456.78 This method has the advantage of being locale-aware. |
|
#5
|
|||
|
|||
|
To my knowledge there's no currency routine or built-in in the bash shell, but you could write a function to take care of it. Also, if this helps, check out this manage for units. It deals mostly with conversion of things like yards and feet, but has some interesting stuff in the middle regarding currency.
www.computerseo.com Last edited by devondad93; 3 Weeks Ago at 10:36 PM.. |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| bash |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| AWK Currency Conversion | 3junior | Shell Programming and Scripting | 3 | 03-21-2009 02:38 PM |