LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Enterprise (https://www.linuxquestions.org/questions/linux-enterprise-47/)
-   -   awk value in floating point value -- want in dollars/cents (https://www.linuxquestions.org/questions/linux-enterprise-47/awk-value-in-floating-point-value-want-in-dollars-cents-609496/)

beeblequix 12-27-2007 05:28 PM

awk value in floating point value -- want in dollars/cents
 
howdy folks,

I wrote a long bash script that logs into the database and runs a number of SQL queries then sends the data into a few reports. I've got it *just* about right and have a small problem.

To calculate a "total" dollar number from US@US, THEM1@US and THEM2@US transactions I run this little command (note that the variables are all data gathered from queries & I'd rather not do the query over again -- just piece it together from the three variables):

dollars_total=`echo $dollars_amount1 $dollars_amount2 $dollars_amount3 | awk '{totaldollars=($1 + $2 + $3)} END {print totaldollars}'`

The command actually works but the output for one value came out like this:
Tot$: 2.67824e+06

I'd really rather the data be formatted in a normal dollars & cents fashion.

Does someone know how I can set a specific format on the data so that after the three fields are added together with awk that the data comes out in $ddddddd.cc format?

Any help appreciated. Thanks.

ß

beeblequix 12-27-2007 05:48 PM

http://www.gnu.org/software/gawk/manual/gawk.html

well, this page is useful. I'll change the "print" to "printf" & use format control letters. I used this one:
...{printf "%2.10e\n", totaldollars}...

...which gave this as output:
Tot$: 2.6782389000e+06

Not quite what I wanted but I'll keep working on it.

David1357 12-27-2007 06:43 PM

Quote:

Originally Posted by beeblequix (Post 3003357)
...{printf "%2.10e\n", totaldollars}...

Try
Code:

{printf "%9.5f\n", totaldollars}
"e" is the format specifier for exponential notation.

beeblequix 12-28-2007 09:10 AM

I only slightly modified that value to "%9.2f/n" so my cents value stays two digits. Thanks for your input. :)

ß

David1357 12-28-2007 02:54 PM

Quote:

Originally Posted by beeblequix (Post 3003873)
I only slightly modified that value to "%9.2f/n" so my cents value stays two digits. Thanks for your input. :)

You may want to add ".005" to your total before truncating the digits. Accountants always round up.


All times are GMT -5. The time now is 07:00 AM.