• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to coerce a real to an integer in X?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to coerce a real to an integer in X?


  • Subject: Re: How to coerce a real to an integer in X?
  • From: email@hidden
  • Date: Fri, 1 Feb 2002 02:26:34 EST

The simple fix (if it works under 1.8x -- the round function):

tell application "Finder"
set x to 4.756
-- if you want it to round up and down; omit this line to just round
down.
set x to x + 0.5
set x to (round (x) rounding down)
end tell

Although the commands may not expressedly coerce real to int, you can always
go back to the old-school way and write your own function to do it (coerce it
into a string, read to the decimal, ignoring the decimal, then recoerce that
answer into an integer).

tell application "Finder"
set x to 3.1415259
set x_as_str_var to x as string
set Test_Length to the length of x_as_str_var
repeat with generic_loop_counter from 1 to Test_Length
if character generic_loop_counter of x_as_str_var = "." then
set number_of_places to (generic_loop_counter - 1)
set generic_loop_counter to 257
end if
end repeat
set y to characters 1 through number_of_places of x_as_str_var as string
set x to y as integer
end tell

Result -> 3


  • Prev by Date: [Meta,OT] OOP and Forks
  • Next by Date: Re: Scripting Additions: Embracing the Horror of Unix
  • Previous by thread: Re: How to coerce a real to an integer in X?
  • Next by thread: Re: How to coerce a real to an integer in X?
  • Index(es):
    • Date
    • Thread