Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Display Modes
  #1  
Old 03-09-2010
Registered User
 

Join Date: Feb 2010
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
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  
Old 03-09-2010
danmero danmero is offline Forum Advisor  
 

Join Date: Nov 2007
Location: H3X
Posts: 1,713
Thanks: 0
Thanked 9 Times in 9 Posts
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
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,472
Thanks: 0
Thanked 3 Times in 3 Posts
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
fpmurphy's Avatar
Moderator
 

Join Date: Dec 2003
Location: /dev/eou
Posts: 2,385
Thanks: 0
Thanked 21 Times in 20 Posts
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 3 Weeks Ago
Registered User
 

Join Date: Jul 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
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
Reply

Bookmarks

Tags
bash

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:35 AM.