简体   繁体   中英

Checking if floating point number is whole number in tcl

I am new in tcl. I am trying to check if a given number is a whole number but can't seem to find a simple way to do this.

So I have a number2 which is checking if it is on grid. If not a whole number then it is not on grid.

set numberOne 7.5
set grid 2.5
set numberTwo [expr ($numberOne/$grid) ]

if {[string is integer -strict $numberTwo} {
    do something
} else {
    do something else
}

The above code does not work for me since the numberTwo is going to be returned as a floating point number (3.0 in this case)

Python has something like this:

x = 7.5
y = 2.5
z = x/y

if z%1 == 0
   then do something
else
   do something else

Is there a way to do something similar in tcl? If not another alternative could be -

  • take the decimal value of the numberTwo and check if it is 0 or non-0

So something that takes 6.555 returns 555 and takes 6.0 and returns 0. I can then do:

if {$value == 0} {then do something} else {do something else}

It seems you may not know about Tcl math functions .

if {$value == floor($value)} {

    // ...
}

This actually is the solution many people are looking for ;)

proc is_float_whole { float } {
  return [expr abs($float - int($float)) > 0 ? 0 : 1]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM