Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

convert float to int ?

1,148 views
Skip to first unread message

Bruno Serra

unread,
Oct 15, 1993, 4:11:49 PM10/15/93
to

What's the simplest way to transform a float to an int in tcl ?

I've thought of :

scan $float %d int

but there sure must be a straighter way...


Bruno.

-----------------------------------------------------------------------
Bruno SERRA | " Please do not be alarmed.
INRIA - projet Pastis | We will be restoring normality
BP 93 | just as soon as we are sure
06902 Sophia Antipolis Cedex | what is normal anyway.
France | Thank you. "
Phone #93 65 78 82 |
Fax #93 65 76 43 |
Email: se...@sophia.inria.fr | The Hitch-Hiker's Guide to the Galaxy
-----------------------------------------------------------------------

George A. Howlett

unread,
Oct 16, 1993, 12:57:04 PM10/16/93
to
Bruno Serra (se...@ctuluh.inria.fr) wrote:

: What's the simplest way to transform a float to an int in tcl ?

: I've thought of :

: scan $float %d int

: but there sure must be a straighter way...

From the Tcl 7.0 "expr" manual page ...

Tcl also implements the |
following functions for conversion between integers and |
floating-point numbers: |


int(arg)
If arg is an integer value, returns arg, otherwise con- |
verts arg to integer by truncation and returns the con- |
verted value. |

round(arg)
If arg is an integer value, returns arg, otherwise con- |
verts arg to integer by rounding and returns the con- |
verted value. |


So you can use "int" or "round" depending on if you want the
real-value truncated or rounded.

set int [expr int($float)]
set int [expr round($float)]

--ga

0 new messages