Printf statement for currency conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printf statement for currency conversion
# 8  
Old 08-26-2014
Hi, hemanthsaikumar . Note that that the printf statement has a format part as the first argument. You need to use that!

Also your awk statement cannot work like that because the content of the shell variable $amount is unavailable to awk because of the single quotes..

Also note that I used the BEGIN section. You are using the middle section, so then awk expects a file to process.

Yes you can combine: awk -F, -v ...
# 9  
Old 08-26-2014
hi ,
actually iam getting the amount value from other file which is a comma seperated file... liek i had a doubt like how to insert this printf statement in the awk command
Code:
awk -F "," '{printf $amount}'

for the above statemnt is der any posibilty of adding printf "%'2f\n" thing ...? to convert the amount ..?
# 10  
Old 08-26-2014
Say the amount is in the 3rd field, you can do something like this:
Code:
awk '{$3=sprintf(curfmt,$3)}1' FS=, OFS=, curfmt="$%'.2f" file

or

Code:
awk '{printf fmt, $1, $2, $3, $4}' FS=, fmt="%s,%s,$%'.2f,%s\n" file

Code:
$ echo 0,2,10000,c | awk '{$3=sprintf(curfmt,$3)}1' FS=, OFS=, curfmt="$%'.2f" 
0,2,$10,000.00,c


---
I specified the format as en external variable because of the single quote, which use gets complicated by the fact that the awk script itself is in single quotes. An alternative is to put the script in a separate file and call it with awk -f

Code:
BEGIN{
  FS=OFS=","
}

{
  $3=sprintf("$%'.2f",$3)
}
1

call it like
Code:
awk -f test.awk file


Last edited by Scrutinizer; 08-26-2014 at 10:41 AM..
# 11  
Old 08-26-2014
Not all awk versions recognize that " ' " flag char, even when the shell's printf does.
# 12  
Old 08-26-2014
Hi.

An excerpt from the useful bash Cookbook: Solutions and Examples for bash Users (Cookbooks (O'Reilly)): Carl Albing, JP Vossen, Cameron Newham: 9780596526788: Amazon.com: Books
Code:
# cookbook filename: func_commify
# bash cookbook, Albig, et al, Page 450.

function commify {
    typeset text=${1}

    typeset bdot=${text%%.*}
    typeset adot=${text#${bdot}}

    typeset i commified
    (( i = ${#bdot} - 1 ))

    while (( i>=3 )) && [[ ${bdot:i-3:1} == [0-9] ]]; do
        commified=",${bdot:i-2:3}${commified}"
        (( i -= 3 ))
    done
    echo "${bdot:0:i+1}${commified}${adot}"
}

So once this shell function is available to the current shell, then this:
Code:
commify 1000

would produce this:
Code:
1,000

on a system like this:
Code:
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39

Best wishes ... cheers, drl
# 13  
Old 08-26-2014
Quote:
Originally Posted by RudiC
Not all awk versions recognize that " ' " flag char, even when the shell's printf does.
It should if it conforms to the POSIX.1-2008 standard...
Quote:
The flag characters and their meanings are:


'
[CX] [Option Start] (The <apostrophe>.) The integer portion of the result of a decimal conversion ( %i, %d, %u, %f, %F, %g, or %G ) shall be formatted with thousands' grouping characters. For other conversions the behavior is undefined. The non-monetary grouping character is used. [Option End]
Quote:
[CX] [Option Start] Extension to the ISO C standard [Option End]
The functionality described is an extension to the ISO C standard. Application developers may make use of an extension as it is supported on all POSIX.1-2008-conforming systems.
printf() function: DESCRIPTION
XBD Fileformat notation
awk: output statements
printf utility: EXTENDED DESCRIPTION


As noted earlier, it is however dependent on the locale if the non-monetary grouping character is defined...
Code:
$ LANG="en_US.UTF-8" awk -v fmt="%'d\n" -v amount=10000 'BEGIN{printf fmt, amount}'
10,000
$ LANG="C" awk -v fmt="%'d\n" -v amount=10000 'BEGIN{printf fmt, amount}'
10000
$ LANG="de_DE.UTF-8" awk -v fmt="%'d\n" -v amount=10000 'BEGIN{printf fmt, amount}'
10000

So it is probably advisable to use LANG="en_US.UTF-8" awk .. where available..
# 14  
Old 08-26-2014
That LANG= doesn't necessarily help:
Code:
LANG="en_US.UTF-8" awk -v fmt="%'d\n" -v amount=10000 'BEGIN{printf fmt, amount}'
awk: run time error: improper conversion(number 1) in printf("%'d
")
    FILENAME="" FNR=0 NR=0

, see also https://www.unix.com/302912434-post26.html
This User Gave Thanks to RudiC For This Post:
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