Format Money/Currency (U.S.)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format Money/Currency (U.S.)
# 1  
Old 03-09-2010
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
# 2  
Old 03-09-2010
You can use both:
Code:
# var=52.5259
# printf "$%.2f\n" $var
$52.53
# echo $var | awk '{printf "$%.2f\n",$1}'
$52.53

all depends on context.
# 3  
Old 03-10-2010
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  
Old 03-10-2010
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  
Old 07-06-2010
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; 07-07-2010 at 11:36 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Programming

Visual Basic converting a decimal data type to a label with currency format

Here is the code that I am working with. I have tried several other things. any suggestions? Lbl_Cost_Output.Text = (dDistance * dCostPerMile).ToString("C") The label is formatted correctly in terms of value 0.00 but no dollar sign appears. Please let me know if you have any questions. (1 Reply)
Discussion started by: briandanielz
1 Replies

2. Shell Programming and Scripting

Printf statement for currency conversion

hi all, I had my script as a=qw b=rter c=fdfd curency=1000 printf"${curency} $a $b $c" > filename can i have printf statement that can change the currency from 1000 to 1,000 like it should convert the number to currency format ..?(i.e for any number) (14 Replies)
Discussion started by: hemanthsaikumar
14 Replies

3. Shell Programming and Scripting

Maintaining file currency

I have a common data folder with files like x* which is accessed by 3 unix servers. Now each server will try to pick one file form this folder and move it to its local folder. How to maintain file concurrency in this case?I dont want the same file to be accessed by more than one process. (2 Replies)
Discussion started by: prasperl
2 Replies

4. Shell Programming and Scripting

Regular expressions to check numbers with currency

Hi All I am struggling for the last one week on how to write a regular expression to search a number with currency (such as $ 1, 245, 000.00, Rs. 1, 00, 000.00 etc) but in vain . Please help me. Thanks in advance. -----Post Update----- Hi All I am struggling for the last one... (7 Replies)
Discussion started by: my_Perl
7 Replies

5. Shell Programming and Scripting

AWK Currency Conversion

How can I use awk command to convert values to currency. For example I have a database like follows John:200 smith:300 kim:405 and want it to out put like this John $200.00 (3 Replies)
Discussion started by: 3junior
3 Replies
Login or Register to Ask a Question