Printf statement for currency conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printf statement for currency conversion
# 1  
Old 08-26-2014
Printf statement for currency conversion

hi all,
I had my script as
Code:
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)
# 2  
Old 08-26-2014
Depending on your locale, you can do something like this:

Code:
$ printf "%'d\n" 1000
1,000

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 08-26-2014
hi,
how can we include this %d tag in my prinf statement exactly like
Code:
a=qw
b=rter
c=fdfd
curency=1000
printf"${curency} $a $b $c" > filename

how to insert in the ablve printf statement..?
# 4  
Old 08-26-2014
Code:
$ a=qw
$ b=rter
$ c=fdfd
$ currency=1000
$ LC_ALL=en_US.UTF8 printf "%'d %s %s %s\n" "$currency" "$a" "$b" "$c" > filename
$ cat filename
1,000 qw rter fdfd

# 5  
Old 08-26-2014
hi
in awk command can we do the same..?
Code:
amount=2000
awk -F "," '{printf $amount}'

can u help me on this..?

---------- Post updated at 06:37 AM ---------- Previous update was at 06:32 AM ----------

hi jlliagre,
thanks for the reply.. the above statement is not working if the currency is at the middle
Code:
a=qw
b=rter
curency=1000
c=fdfd
printf"$a $b ${curency}  $c" > filename

my requirement is it should be in the middle
# 6  
Old 08-26-2014
Quote:
Originally Posted by hemanthsaikumar
hi
in awk command can we do the same..?
Sure:
Code:
amount=1000
awk -v fmt="%'d\n" -v amount="$amount" 'BEGIN{printf fmt, amount}'


Last edited by Scrutinizer; 08-26-2014 at 08:42 AM..
# 7  
Old 08-26-2014
hi Scrutinizer,
i had my script as defined like my awk command is
Code:
amount=2000
awk -F "," '{printf $amount}'

i have to convert $amount to currency mine is a comma delimiter file aim getting value from that file like amount=$2

---------- Post updated at 07:11 AM ---------- Previous update was at 06:46 AM ----------

hi Scrutinizer,
as my awk command is -F (field sepaerator) but u wrote using -v but my awk command should have -F too is that possible ..?
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add printf statement in awk command?

hi all i need to add the prinf statement in awk command for the converted comma separated output.... below is my code : Code Credits :RudiC awk -F, 'NF==2 {next} {ITM=$1 AMT=$2+0 CNT=$3+0 TOTA+=$2 ... (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

2. Programming

Printf conversion specifiers

Hello, this is one examples that I always panic with C printf format specifier. 1) I did read the manpage with man 3 printf ...... One can also specify explicitly which argument is taken, at each place where an argument is required, by writing "%m$" instead of '%' and "*m$"... (10 Replies)
Discussion started by: yifangt
10 Replies

3. Shell Programming and Scripting

creating printf statement using user arguments

I am writing a script in bash and want to perform the operation I check number of arguments and make a print statement with the passes arguments If I pass 3 arguments I will do printf "$frmt" "$1" "$2" "$3"If I have 4 arguments I do printf "$frmt" "$1" "$2" "$3" "$4"etc (4 Replies)
Discussion started by: kristinu
4 Replies

4. Shell Programming and Scripting

NAWK conversion of hexadecimal input to decimal output via printf, I am close I can feel it

I have searched and the answers I have found thus far have led me to this point, so I feel I am just about there. I am trying to convert a column of hexadecimal to decimal values so that I can filter out via grep just the data I want. I was able to pull my original 3 character hex value and... (10 Replies)
Discussion started by: PCGameGuy
10 Replies

5. 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

6. Shell Programming and Scripting

Printf statement

The printf statement pay_amount=$(printf "%013.3f" "$4") working perfectly at one path(xxx/home/rsh) and showing error (printf: 216.000: invalid number) at another path(/opt/xxxx/xxxx). what will be the reason? thanks in advance (4 Replies)
Discussion started by: reeta_shri
4 Replies

7. Shell Programming and Scripting

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 (4 Replies)
Discussion started by: Ccccc
4 Replies

8. 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