Currency format in Bash Script

Thread Solved

Join Date: May 2008
Posts: 3
Reputation: prabowoadis is an unknown quantity at this point 
Solved Threads: 0
prabowoadis prabowoadis is offline Offline
Newbie Poster

Currency format in Bash Script

 
0
  #1
Jun 5th, 2008
Hai there,

I would like to ask about currency format in Bash script

let say i have $US2550000, after format it, it become $US2.550.000,00

Thanks for the help
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Currency format in Bash Script

 
0
  #2
Jun 10th, 2008
Hey There,

How are you formatting?

locale -a should show you all the locales available, and you could use that to set your environment

LC_ALL=[INSERT_YOUR_PREFERRED_LOCALE_FROM_local-a_ABOVE] locale charmap

should set your formatting correctly.

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 3
Reputation: prabowoadis is an unknown quantity at this point 
Solved Threads: 0
prabowoadis prabowoadis is offline Offline
Newbie Poster

Re: Currency format in Bash Script

 
0
  #3
Jun 17th, 2008
hi mike
is bash script have built in function for currency format?Because i want to format inside the program
let say when i want to format the value i just call the function formatCurr(value)

thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Currency format in Bash Script

 
0
  #4
Jun 17th, 2008
Hey again,

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 manpage for "units" - it deals mostly with conversion of things like yards and feet, but has some interesting stuff in the middle regarding currency. It might be a good place to start.

Let me know how you come along. If you want, post again with (as specific as possible, what you want to do (like how the currency should format, etc). I'd be interested in helping you out for selfish reasons as well. I'm not sure how to do it and it would make for a good exercise

Best wishes,

Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 58
Reputation: omrsafetyo is an unknown quantity at this point 
Solved Threads: 9
omrsafetyo omrsafetyo is offline Offline
Junior Poster in Training

Re: Currency format in Bash Script

 
0
  #5
Jun 18th, 2008
Try the following out:

Shell Scripting Syntax (Toggle Plain Text)
  1. function formatCurr {
  2. dollar_amt=$1
  3. length=`echo $dollar_amt | awk '{ print length($0) }'`
  4. mod=`expr $length % 3`
  5. div3=`expr $length / 3`
  6. if [[ $mod -ne 0 ]]
  7. then
  8. dollar_pt0=`echo $dollar_amt | cut -c 1-$mod`
  9. fi
  10.  
  11. dollar_fin=`echo "${dollar_pt0}"`
  12.  
  13. modp1=`expr $mod + 1`
  14. incr=`expr $mod + 3`
  15.  
  16. for (( i = 0; i < $div3; i++ ))
  17. do
  18. mySub=`echo $dollar_amt | cut -c ${modp1}-${incr}`
  19. if [[ $modp1 -ne 1 ]]
  20. then
  21. dollar_fin=`echo ${dollar_fin},${mySub}`
  22. else
  23. dollar_fin=`echo ${dollar_fin}${mySub}`
  24. fi
  25. incr=`expr $incr + 3`
  26. modp1=`expr $modp1 + 3`
  27. done
  28. dollar_fin=`echo "$"$dollar_fin`
  29. echo $dollar_fin
  30. }
I just wrote this ugly piece of code up - and it should do exactly what you are looking for - unless of course you need to parse cents - in which case its going to need some extra work.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 58
Reputation: omrsafetyo is an unknown quantity at this point 
Solved Threads: 9
omrsafetyo omrsafetyo is offline Offline
Junior Poster in Training

Re: Currency format in Bash Script

 
0
  #6
Jun 19th, 2008
A final script that parses cents, if input using a dot ("."):

  1. function formatCurr {
  2. dollar_amt=$1
  3.  
  4. cents=`echo $dollar_amt | grep '\.'`
  5. if [[ "x${cents}x" != "xx" ]]
  6. then
  7. cent_amt=`echo $dollar_amt | cut -d"." -f2 | cut -c 1-2`
  8. dollar_amt=`echo $dollar_amt | cut -d"." -f1`
  9. cent_amt=`echo ".$cent_amt"`
  10. fi
  11.  
  12. length=`echo $dollar_amt | awk '{ print length($0) }'`
  13. mod=`expr $length % 3`
  14. div3=`expr $length / 3`
  15.  
  16. if [[ $mod -ne 0 ]]
  17. then
  18. dollar_fin=`echo $dollar_amt | cut -c 1-$mod`
  19. fi
  20.  
  21. modp1=`expr $mod + 1`
  22. incr=`expr $mod + 3`
  23.  
  24. for (( i = 0; i < $div3; i++ ))
  25. do
  26. mySub=`echo $dollar_amt | cut -c ${modp1}-${incr}`
  27. if [[ $modp1 -ne 1 ]]
  28. then
  29. dollar_fin=`echo ${dollar_fin},${mySub}`
  30. else
  31. dollar_fin=`echo ${dollar_fin}${mySub}`
  32. fi
  33. incr=`expr $incr + 3`
  34. modp1=`expr $modp1 + 3`
  35. done
  36.  
  37. if [[ "x${cent_amt}x" != "xx" ]]
  38. then
  39. dollar_fin=`echo ${dollar_fin}${cent_amt}`
  40. fi
  41.  
  42. echo "$"$dollar_fin
  43. }
This script is based on the idea that the first comma has to come after (numberLength) mod 3 digits... E.g., if the digit is 9 characters, 9 % 3 = 0. In the event of 0, the first comma is skipped. 10 % 3 = 1, so after the 1st character, we place a comma. Subsequently, we place a comma after every 3rd character. (numberLength) / 3 = the number of commas that need to be placed, so for 0 to (numberLength) / 3 we grab three characters starting at the last position we left off at and then insert a comma.

It parses cents by looking for the ".". If it exists, it assigns the cent amount, and the dollar amount. We check and make sure cents are no more than 2 digits. We then assign a period to the beginning of the cents - I did this because otherwise at the bottom when you combine dollars with cents you would have to determine whether or not cents has been defined - this just makes one less logic to look for.
At the end you echo a dollar sign with dollars and cents trailing - this is the return for the function, so you can assign it as such:

dollar_format=`formatCurr(rawAmount)`

Let me know if you find an error.
Last edited by omrsafetyo; Jun 19th, 2008 at 9:30 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 3
Reputation: prabowoadis is an unknown quantity at this point 
Solved Threads: 0
prabowoadis prabowoadis is offline Offline
Newbie Poster

Re: Currency format in Bash Script

 
0
  #7
Jun 25th, 2008
Hi Mike and omrsafetyo

#Mike, your solution to set up the environment work well

#omrsafetyo , the 2nd solutin work well, i've modified a little bit of your code

Thanks guys
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Currency format in Bash Script

 
0
  #8
Jun 25th, 2008
Rock and roll,

That's awesome that you've got two workable options now!!

And, thanks to omrsafetyo, from myself. Nice work. Hasn't produced an error yet

Glad to help out

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 1
Reputation: adrianchav is an unknown quantity at this point 
Solved Threads: 0
adrianchav adrianchav is offline Offline
Newbie Poster

Re: Currency format in Bash Script

 
0
  #9
Nov 6th, 2008
Hi ormsafetyo,

I'am just starting with shells and i'am having problems with my first one.
I need to write a shell script that receives 3 parameters (Filename, Item Description, and Amount)
when the script is invoked, it is suppossed to insert the "Item Description" and "Amount" to the the exsisting text file, I guess is working fine, but I need to format the number from this 12530 to this 12,530 or from this 1225550 to this 1,225,550

I'am trying to use your function formatCurr but I guess I don't know how to use it in the correct way or even how to call the function

I will really appreciate if you can help me with this!!!!!!!

THIS IS THE ERROR I GET WHEN RUNNING THE SCRIPT:
----------------------------------------------------------------------
B0581342 >prueba.sh 'adrian.txt' 'item description' '12530'
UX:sh (prueba.sh): ERROR: prueba.sh: Syntax error at line 1: `formatCurr' unexpected
B0581342 >more adrian.txt
item description 12530
B0581342 >

THE FOLLOWING IS THE SCRIPT:
--------------------------------------------
  1. #!/bin/sh
  2. ################################################################
  3.  
  4. LOCALDIR=/usrclass/soa/
  5.  
  6. cd /usrclass/soa/
  7. cd $LOCALDIR
  8.  
  9. FAXFILE=$1
  10. DESRUBRO=$2
  11. MONRUBRO=$3
  12. RUBRO=$2
  13. MONTO=$3
  14.  
  15. if [ ! -f "$FAXFILE" ]; then
  16. touch "$FAXFILE" # cria o arquivo de LOG se ele nM-co existe
  17. else
  18. if [ `ls -al $FAXFILE | awk '{print $5}'` -gt 5000000 ]; then
  19. mv $FAXFILE `ls $FAXFILE | cut -f1 -d .`".old"
  20. touch "$FAXFILE"
  21. fi
  22.  
  23. fi
  24.  
  25. CONT_RUBRO=40
  26. CONT_MONTO=15
  27. ESPACIOS_RUBRO=" "
  28. ESPACIOS_MONTO=" "
  29. LINEA_RUBRO=" "
  30.  
  31. lenrubro=`echo $DESRUBRO | wc -c`
  32. lenrubro=`expr $lenrubro - 1`
  33.  
  34. lenmonto=`echo $MONRUBRO | wc -c`
  35. lenmonto=`expr $lenmonto - 1`
  36.  
  37. if [ $lenrubro -lt $CONT_RUBRO ]; then
  38. while [ $lenrubro -lt $CONT_RUBRO ]
  39. do
  40. ESPACIOS_RUBRO="$ESPACIOS_RUBRO"" "
  41. lenrubro=`expr $lenrubro + 1`
  42. done
  43. RUBRO=$RUBRO$ESPACIOS_RUBRO
  44. fi
  45.  
  46. if [ $lenmonto -lt $CONT_MONTO ]; then
  47. while [ $lenmonto -lt $CONT_MONTO ]
  48. do
  49. ESPACIOS_MONTO="$ESPACIOS_MONTO"" "
  50. lenmonto=`expr $lenmonto + 1`
  51. done
  52. MONTO=$ESPACIOS_MONTO$MONTO
  53. fi
  54.  
  55. LINEA_RUBRO=$RUBRO$MONTO
  56. echo "$LINEA_RUBRO" >> $FAXFILE
  57.  
  58. #****************************************************
  59. #THESE ARE THE TWO LINES I ADDED TO TRY TO USE THE FUNCTION
  60. #I ALSO TRY TO USE THESE TWO LINES AT THE END
  61.  
  62. dollar_format=`formatCurr("$MONRUBRO")`
  63. echo "$dollar_format" >> $FAXFILE
  64. #********************************************************
  65.  
  66. #*******************************************************
  67. function formatCurr {
  68. dollar_amt=$3
  69.  
  70. cents=`echo $dollar_amt | grep '\.'`
  71. if [[ "x${cents}x" != "xx" ]]
  72. then
  73. cent_amt=`echo $dollar_amt | cut -d"." -f2 | cut -c 1-2`
  74. dollar_amt=`echo $dollar_amt | cut -d"." -f1`
  75. cent_amt=`echo ".$cent_amt"`
  76. fi
  77.  
  78. length=`echo $dollar_amt | awk '{ print length($0) }'`
  79. mod=`expr $length % 3`
  80. div3=`expr $length / 3`
  81.  
  82. if [[ $mod -ne 0 ]]
  83. then
  84. dollar_fin=`echo $dollar_amt | cut -c 1-$mod`
  85. fi
  86.  
  87. modp1=`expr $mod + 1`
  88. incr=`expr $mod + 3`
  89.  
  90. for (( i = 0; i < $div3; i++ ))
  91. do
  92. mySub=`echo $dollar_amt | cut -c ${modp1}-${incr}`
  93. if [[ $modp1 -ne 1 ]]
  94. then
  95. dollar_fin=`echo ${dollar_fin},${mySub}`
  96. else
  97. dollar_fin=`echo ${dollar_fin}${mySub}`
  98. fi
  99. incr=`expr $incr + 3`
  100. modp1=`expr $modp1 + 3`
  101. done
  102.  
  103. if [[ "x${cent_amt}x" != "xx" ]]
  104. then
  105. dollar_fin=`echo ${dollar_fin}${cent_amt}`
  106. fi
  107.  
  108. echo "$"$dollar_fin
  109. }
  110.  
  111. #*******************************************************
  112.  
  113. dollar_format=`formatCurr(MONRUBRO)`
  114. echo "$dollar_format" >> $FAXFILE
Last edited by cscgal; Nov 6th, 2008 at 12:18 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 58
Reputation: omrsafetyo is an unknown quantity at this point 
Solved Threads: 9
omrsafetyo omrsafetyo is offline Offline
Junior Poster in Training

Re: Currency format in Bash Script

 
0
  #10
Nov 6th, 2008
Your first problem is that you need to define the function above where you call it from - so you should have a functions section at the top of your script, which is where my code should be. I'm assuming your script is trying to run the function on line 62 and running into the trouble there.

Secondly, you would need to modify the script so that it doesn't output the dollar sign, or the cents; which means, probably cut out from lines 70-76 and 103-106; and change 108 to "echo $dollar_fin".

You might also want to change the variable names and function name to reflect that you are working with numbers and not dollars.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC