egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Decimal numbers

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
IRCNick
Halfop


Joined: 12 Oct 2005
Posts: 64
Location: Germany

PostPosted: Sun Jul 02, 2006 5:16 am    Post subject: Decimal numbers Reply with quote

Hi, I have a problem with the decimal numbers. How can make my script to show and calculate decimal numbers ? Should I use the package "Decimal arithmetic" or there is other way to do what I need ?

This is a sample of that what I'm trying to do:

Code:

set a 12; set b 20; set sum [expr ($a + $b)]
set c [expr (($a / 100) * ($sum / 100)) * 100]
set d [expr (($b / 100) * ($sum / 100)) * 100]
Back to top
View user's profile Send private message Visit poster's website
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Sun Jul 02, 2006 6:37 am    Post subject: Reply with quote

Add a decimal point to the number you're dividing by. For example:
Code:
set c [expr (($a / 100.) * ($sum / 100.)) * 100]

_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
IRCNick
Halfop


Joined: 12 Oct 2005
Posts: 64
Location: Germany

PostPosted: Mon Jul 03, 2006 3:06 pm    Post subject: Reply with quote

Ok it works, but now I have a double value for the result. How can I convert it to integer ?
Back to top
View user's profile Send private message Visit poster's website
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Mon Jul 03, 2006 5:27 pm    Post subject: Reply with quote

Code:
set result [expr {int($result)}]

_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
IRCNick
Halfop


Joined: 12 Oct 2005
Posts: 64
Location: Germany

PostPosted: Mon Jul 03, 2006 6:34 pm    Post subject: Reply with quote

Thank you
Back to top
View user's profile Send private message Visit poster's website
De Kus
Revered One


Joined: 15 Dec 2002
Posts: 1361
Location: Germany

PostPosted: Thu Jul 06, 2006 7:07 am    Post subject: Reply with quote

int() will cut off string after the decimal point, round() will do the same, but adding 0.5 before doing so Very Happy.
_________________
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
Back to top
View user's profile Send private message MSN Messenger
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Fri Jul 07, 2006 5:03 am    Post subject: Reply with quote

round() will round to the closest number. For example:
Quote:
% expr round(1.2)
1
% expr round(1.6)
2

floor() will round to the lower and ceil() will round to the greater.
Quote:
% expr floor(1.6)
1.0
% expr ceil(1.6)
2.0

_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber