Korn Shell manipulating the string into dynamic currency number

Conversion of string into currency value..

ex1:

number_of_positions=2
input_string=345987

Output= 345,987.00

ex2:

number_of_positions=4
input_string=1345987

Output= 1,345,987.0000

Please respond as soon as possible

What have you tried?

Is this a homework assignment?

Homework and coursework questions can only be posted in this forum under special homework rules.

1 Like

This is my work related.. in development we have bash shell... below command is working fine.. but in prod environment we have korn shell and the same command is failing..

COL_VAL_2=`printf "%'0.${V_NO_of_Dec}f" $COL_VAL_1`

here
V_NO_of_Dec is the number of decimals that needed in output
COL_VAL_1 is input value

it is prod issue.. please help me out..

What operating system are you using? What version of the Korn shell are you using?

On OS X, with ksh version sh (AT&T Research) 93u+ 2012-08-01 , the following script (using the variables you specified in your 1st post):

#!/bin/ksh
for number_of_positions in 2 4
do	for input_string in 345987 1345987
	do	output_string=$(printf "%'.*f\n" $number_of_positions \
			$input_string)
		printf 'number_of_positions=%d\n' $number_of_positions
		printf 'input_string=%s\n' $input_string
		printf 'Output=%s\n\n' "$output_string"
	done
done

produces the output:

number_of_positions=2
input_string=345987
Output=345,987.00

number_of_positions=2
input_string=1345987
Output=1,345,987.00

number_of_positions=4
input_string=345987
Output=345,987.0000

number_of_positions=4
input_string=1345987
Output=1,345,987.0000

And, it produces the same output with or without the zero in the printf format string before the decimal point.

PS. In what way is ksh failing? What diagnostic does it print?

1 Like

Thanks for prompt response..

copied same code into sample script and executed. below is the output i see.. i just pasted for one value..

+ + printf %'.*f\n 2 345987
printf: bad conversion
printf: bad conversion
output_string=.*f
.*f
+ printf number_of_positions=%d\n 2
number_of_positions=2
+ printf input_string=%s\n 345987
input_string=345987
+ printf Output=%s\n\n .*f
.*f
Output=.*f
.*f

And also to give more information--
OS version - AIX version 6.1
Korn Shell version - Version M-11/16/88f

Please advice to add if any binaries or do the needful..

Thanks

Possible that printf version doesn't like the ' ?

I repeat: What operating system are you using and what version of the Korn shell are you using?

1 Like

Sorry!! edited my last reply bit late..

Here is the same information..

OS version - AIX version 6.1
Korn Shell version - Version M-11/16/88f

I believe AIX comes with both ksh and ksh93 . Try using ksh93 instead of ksh . Also, if you can't use ksh93 , try /usr/bin/printf in the ksh script instead of just printf when you have ' as a flag in your printf format strings.

1 Like

same issue.. used /usr/bin/printf instead of printf

+ + /usr/bin/printf %'.*f\n 2 345987
printf: bad conversion
printf: bad conversion
output_string=.*f
.*f
+ printf number_of_positions=%d\n 2
number_of_positions=2
+ printf input_string=%s\n 345987
input_string=345987
+ printf Output=%s\n\n .*f
.*f
Output=.*f
.*f

What about trying it with ksh93 instead of ksh ?

1 Like

we have only ksh..

below is the work with hard coded value.. resulted good.. but can you use the below command into your code and try it.. because I'm not able to use your code with the below command.

export LC_NUMERIC=en_US
$ echo "a" | awk '{printf ("%'\''2.2f",10000.1717)}'

---------- Post updated at 07:47 PM ---------- Previous update was at 07:09 PM ----------

Done with it.

echo "" | awk -v v1="10" '{printf ("%'\''2.2f",v1)}'

Thanks for your help Don..

If you have a bunch of these to do in a loop, you might want something more like:

#!/bin/ksh
for number_of_positions in 2 4
do	for input_string in 345987 1345987
	do	printf "%s %s\n" $number_of_positions $input_string
	done
done | awk -v fmt="%'.*f\n" '
{	printf("\nDecimal Places=%s\nInput=%s\nOutput=", $1, $2)
	printf(fmt, $1, $2)
}'

I assume you won't need the 1st printf in your awk script, but with a setup like this you can pass the number of decimal places you want and your column 1 values in on separate lines and get a line of output for the corresponding column 2 value you're trying to format.

Just firing up awk once to process an entire column of data is a lot more efficient than invoking awk once for each value.

If for some reason you have to do awk for a single value, you can at least get rid of the pipeline by using:

#!/bin/ksh
for number_of_positions in 2 4
do	for input_string in 345987 1345987
	do	awk -v fmt="%'.${number_of_positions}f\n" -v v="$input_string" 'BEGIN{printf(fmt, v)}'
	done
done
1 Like

Tried with both the codes mentioned.. output is not having commas whereas I'm expecting to be as currency field..

first code:

Decimal Places=2
Input=345987
Output=345987.00
Decimal Places=2
Input=1345987
Output=1345987.00
Decimal Places=4
Input=345987
Output=345987.0000
Decimal Places=4
Input=1345987
Output=1345987.0000

second code:

345987.00
1345987.00
345987.0000
1345987.0000

The working of the single quote in the printf format statement depends on whether a thousands separator is defined in the locale

$ ( export LC_NUMERIC=en_US.UTF-8 ; printf "%'.2f\n" 123456 )
123,456.00
$ ( export LC_NUMERIC=C ; printf "%'.2f\n" 123456 )
123456.00

You would need to find out if there are suitable locale on your system (you can list them with locale -a ).

1 Like

Much thanks to Don Cragun.. I just added locale variable to his last code and it is working fine..

export LC_NUMERIC=en_US