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

Recommended Answers

All 9 Replies

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

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

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

Try the following out:

function formatCurr {
dollar_amt=$1
length=`echo $dollar_amt | awk '{ print length($0) }'`
mod=`expr $length % 3`
div3=`expr $length / 3`
if [[ $mod -ne 0 ]]
then
  dollar_pt0=`echo $dollar_amt | cut -c 1-$mod`
fi

dollar_fin=`echo "${dollar_pt0}"`

modp1=`expr $mod + 1`
incr=`expr $mod + 3`

for (( i = 0; i < $div3; i++ ))
do
   mySub=`echo $dollar_amt | cut -c ${modp1}-${incr}`
   if [[ $modp1 -ne 1 ]]
   then
     dollar_fin=`echo ${dollar_fin},${mySub}`
   else
     dollar_fin=`echo ${dollar_fin}${mySub}`
   fi
   incr=`expr $incr + 3`
   modp1=`expr $modp1 + 3`
done
dollar_fin=`echo "$"$dollar_fin`
echo $dollar_fin
}

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.

A final script that parses cents, if input using a dot ("."):

function formatCurr {
dollar_amt=$1

cents=`echo $dollar_amt | grep '\.'`
if [[ "x${cents}x" != "xx" ]]
then
  cent_amt=`echo $dollar_amt | cut -d"." -f2 | cut -c 1-2`
  dollar_amt=`echo $dollar_amt | cut -d"." -f1`
  cent_amt=`echo ".$cent_amt"`
fi

length=`echo $dollar_amt | awk '{ print length($0) }'`
mod=`expr $length % 3`
div3=`expr $length / 3`

if [[ $mod -ne 0 ]]
then
  dollar_fin=`echo $dollar_amt | cut -c 1-$mod`
fi

modp1=`expr $mod + 1`
incr=`expr $mod + 3`

for (( i = 0; i < $div3; i++ ))
do
   mySub=`echo $dollar_amt | cut -c ${modp1}-${incr}`
   if [[ $modp1 -ne 1 ]]
   then
     dollar_fin=`echo ${dollar_fin},${mySub}`
   else
     dollar_fin=`echo ${dollar_fin}${mySub}`
   fi
   incr=`expr $incr + 3`
   modp1=`expr $modp1 + 3`
done

if [[ "x${cent_amt}x" != "xx" ]]
then
  dollar_fin=`echo ${dollar_fin}${cent_amt}`
fi

echo "$"$dollar_fin
}

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.

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

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

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:
--------------------------------------------

#!/bin/sh
################################################################

LOCALDIR=/usrclass/soa/

cd /usrclass/soa/
cd $LOCALDIR

FAXFILE=$1
DESRUBRO=$2
MONRUBRO=$3
RUBRO=$2
MONTO=$3

if [ ! -f "$FAXFILE" ]; then
        touch "$FAXFILE" # cria o arquivo de LOG se ele nM-co existe
else
        if [ `ls -al $FAXFILE | awk '{print $5}'` -gt 5000000 ]; then
                mv $FAXFILE `ls $FAXFILE | cut -f1 -d .`".old"
                touch "$FAXFILE"
        fi

fi

CONT_RUBRO=40
CONT_MONTO=15
ESPACIOS_RUBRO=" "
ESPACIOS_MONTO=" "
LINEA_RUBRO=" "

lenrubro=`echo $DESRUBRO | wc -c`
lenrubro=`expr $lenrubro - 1`

lenmonto=`echo $MONRUBRO | wc -c`
lenmonto=`expr $lenmonto - 1`

if [ $lenrubro -lt $CONT_RUBRO ]; then
while [ $lenrubro -lt $CONT_RUBRO ]
do
        ESPACIOS_RUBRO="$ESPACIOS_RUBRO"" "
        lenrubro=`expr $lenrubro + 1`
done
        RUBRO=$RUBRO$ESPACIOS_RUBRO
fi

if [ $lenmonto -lt $CONT_MONTO ]; then
while [ $lenmonto -lt $CONT_MONTO ]
do
        ESPACIOS_MONTO="$ESPACIOS_MONTO"" "
        lenmonto=`expr $lenmonto + 1`
done
        MONTO=$ESPACIOS_MONTO$MONTO
fi

        LINEA_RUBRO=$RUBRO$MONTO
        echo "$LINEA_RUBRO" >> $FAXFILE

#****************************************************
#THESE ARE THE TWO LINES I ADDED TO TRY TO USE THE FUNCTION
#I ALSO TRY TO USE THESE TWO LINES AT THE END

dollar_format=`formatCurr("$MONRUBRO")`
echo "$dollar_format" >> $FAXFILE
#********************************************************

#*******************************************************
function formatCurr {
dollar_amt=$3

cents=`echo $dollar_amt | grep '\.'`
if [[ "x${cents}x" != "xx" ]]
then
  cent_amt=`echo $dollar_amt | cut -d"." -f2 | cut -c 1-2`
  dollar_amt=`echo $dollar_amt | cut -d"." -f1`
  cent_amt=`echo ".$cent_amt"`
fi

length=`echo $dollar_amt | awk '{ print length($0) }'`
mod=`expr $length % 3`
div3=`expr $length / 3`

if [[ $mod -ne 0 ]]
then
  dollar_fin=`echo $dollar_amt | cut -c 1-$mod`
fi

modp1=`expr $mod + 1`
incr=`expr $mod + 3`

for (( i = 0; i < $div3; i++ ))
do
   mySub=`echo $dollar_amt | cut -c ${modp1}-${incr}`
   if [[ $modp1 -ne 1 ]]
   then
     dollar_fin=`echo ${dollar_fin},${mySub}`
   else
     dollar_fin=`echo ${dollar_fin}${mySub}`
   fi
   incr=`expr $incr + 3`
   modp1=`expr $modp1 + 3`
done

if [[ "x${cent_amt}x" != "xx" ]]
then
  dollar_fin=`echo ${dollar_fin}${cent_amt}`
fi

echo "$"$dollar_fin
}

#*******************************************************

dollar_format=`formatCurr(MONRUBRO)`
echo "$dollar_format" >> $FAXFILE

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.