LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Enterprise Linux Forums > Linux - Enterprise
User Name
Password
Linux - Enterprise This forum is for all items relating to using Linux in the Enterprise.

Notices


Reply
  Search this Thread
Old 12-27-2007, 05:28 PM   #1
beeblequix
Member
 
Registered: Oct 2005
Location: Tierra Firma, Earth
Distribution: Debian of course...
Posts: 198

Rep: Reputation: 30
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.

ß

Last edited by beeblequix; 12-27-2007 at 05:30 PM.
 
Old 12-27-2007, 05:48 PM   #2
beeblequix
Member
 
Registered: Oct 2005
Location: Tierra Firma, Earth
Distribution: Debian of course...
Posts: 198

Original Poster
Rep: Reputation: 30
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.
 
Old 12-27-2007, 06:43 PM   #3
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Post

Quote:
Originally Posted by beeblequix View Post
...{printf "%2.10e\n", totaldollars}...
Try
Code:
{printf "%9.5f\n", totaldollars}
"e" is the format specifier for exponential notation.
 
Old 12-28-2007, 09:10 AM   #4
beeblequix
Member
 
Registered: Oct 2005
Location: Tierra Firma, Earth
Distribution: Debian of course...
Posts: 198

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

ß
 
Old 12-28-2007, 02:54 PM   #5
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by beeblequix View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
floating point number vijraj Programming 2 03-07-2007 04:17 PM
floating-point in script mitsos Programming 6 08-31-2006 07:14 AM
how can I invoke "awk" from shell to do floating point math? Joseph Schiller Programming 8 01-12-2006 05:00 AM
Floating Point in kernel 2.4.28 lucs Slackware 1 10-19-2005 08:33 AM
managing floating point vince_2x Linux - General 2 09-21-2004 09:05 PM

LinuxQuestions.org > Forums > Enterprise Linux Forums > Linux - Enterprise

All times are GMT -5. The time now is 11:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration