LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-17-2004, 10:03 AM   #1
philipina
Member
 
Registered: May 2003
Posts: 77

Rep: Reputation: 15
How to use a float in a script.


Hello,

I have the following code :

NewAmount=120
for ((i=0;i<2;i++))
do
NewAmount=$((NewAmount / 10))
echo "LOOP"
done
echo "NewAmount=" $NewAmount

In ths case amount = 1 instead of 1.20

How can I solve that?

Thanks in advance.
 
Old 03-17-2004, 01:28 PM   #2
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
use bc or awk, see this thread:

http://www.linuxquestions.org/questi...hreadid=153885
 
Old 03-18-2004, 01:16 AM   #3
philipina
Member
 
Registered: May 2003
Posts: 77

Original Poster
Rep: Reputation: 15
Thanks a lot.
 
Old 03-18-2004, 01:26 AM   #4
philipina
Member
 
Registered: May 2003
Posts: 77

Original Poster
Rep: Reputation: 15
Something is strange.

If a use the amout 123456

If I pass 4 time in the following line (using %7.6) the result is 12.345600 and this is OK.
NewAmount=`echo $NewAmount | awk '{printf("%7.6f\n",$1/10)}'`

But if I replace %7.6 by %7.2 to limit the number of decimal, the result is 12.35 instead of 12.34!

Strange, very strange!!!

Allan.
 
Old 03-18-2004, 08:06 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
you again! ;-)

why is it strange?

it's obviously rounding to the nearest, not just truncating.


billy
 
  


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
Converting int to float? pirate_pete Programming 5 09-15-2005 02:00 AM
[C++] char * to float Ossar Programming 2 08-03-2005 10:28 AM
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 04:52 PM
how big is a float in C? SciYro Programming 11 04-04-2005 09:24 AM
C newbie float ? bluesky Programming 7 09-03-2003 12:51 AM


All times are GMT -5. The time now is 10:32 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration