LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-21-2003, 05:48 AM   #1
rkrijger
LQ Newbie
 
Registered: Mar 2003
Posts: 17

Rep: Reputation: 0
How to display formatted floats


Can anyone help me with this issue?!?

I have a variable in my shell script that has a pretty large value, and I want to echo it to stdout. But this echoed value should be the original value divided by 100 and only 2 digits after the '.'.

How do I do that?!?

I have tried some using:

printf %4.2
bc -l
expr

But none works...

I want to used it a lot in my shell scripts, so maybe I should use a self defined function...

Any help would be appreciated!
 
Old 10-21-2003, 07:07 AM   #2
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
Don't know what shell you're using but the following is how you do it in tcsh

printf "%0.2f\n", variable

John
 
Old 10-21-2003, 09:27 AM   #3
rkrijger
LQ Newbie
 
Registered: Mar 2003
Posts: 17

Original Poster
Rep: Reputation: 0
Re: How to display formatted floats

Thank you for your help. To print a single var is indeed no problem. But I want the var / 1000. And when I use expr, the expression is truncated to int...
 
Old 10-21-2003, 09:49 AM   #4
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
Yes, you're right. Sorry, not enough experimentation on my part.

The following works for me in a foreach loop...

perl -e 'printf "%0.2f\n", $ARGV[0]/1000' $var

John
 
Old 10-21-2003, 09:52 AM   #5
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
you can also use awk:

abc=12345
echo $abc | awk '{printf("%4.2f\n", $1/100)}'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
LaTeX Gurus - figures, floats, and stuff FunkyRes Linux - Software 1 08-26-2005 06:03 PM
Perl, integer and floats ivanatora Programming 2 04-06-2005 06:09 PM
byte swapping for floats hypnoticSpectre Programming 1 11-08-2004 11:48 PM
Formatted output with g++ boa Programming 3 11-25-2003 10:57 AM
what if i formatted it vfat ? RHlinux9 Linux - Newbie 4 09-16-2003 12:57 AM


All times are GMT -5. The time now is 06:35 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration