| View previous topic :: View next topic |
| Author |
Message |
IRCNick Halfop
Joined: 12 Oct 2005 Posts: 64 Location: Germany
|
Posted: Sun Jul 02, 2006 5:16 am Post subject: Decimal numbers |
|
|
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 |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Sun Jul 02, 2006 6:37 am Post subject: |
|
|
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 |
|
 |
IRCNick Halfop
Joined: 12 Oct 2005 Posts: 64 Location: Germany
|
Posted: Mon Jul 03, 2006 3:06 pm Post subject: |
|
|
| Ok it works, but now I have a double value for the result. How can I convert it to integer ? |
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
|
| Back to top |
|
 |
IRCNick Halfop
Joined: 12 Oct 2005 Posts: 64 Location: Germany
|
Posted: Mon Jul 03, 2006 6:34 pm Post subject: |
|
|
| Thank you |
|
| Back to top |
|
 |
De Kus Revered One

Joined: 15 Dec 2002 Posts: 1361 Location: Germany
|
Posted: Thu Jul 06, 2006 7:07 am Post subject: |
|
|
int() will cut off string after the decimal point, round() will do the same, but adding 0.5 before doing so . _________________ 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 |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Fri Jul 07, 2006 5:03 am Post subject: |
|
|
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 |
|
 |
|