• 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: Number of decimal places
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Number of decimal places


  • Subject: Re: Number of decimal places
  • From: email@hidden
  • Date: Mon, 22 Apr 2002 17:03:37 -0600

What a fun challenge! I am sure someone will come up with a mathematical
algorithm to do the job in one line, but the following is how I would do
it. But I had to cheat. Notice that I removed the coercion of the string
value to a real out of the first line. First, to keep this coercion, you
would have to trap to make sure the user enters a string that can be
coerced to a number. It is much simpler to leave the value as a string and
do string manipulation. My solution does not test whether the value is
coerceable to a number, it just returns the number of places the decimal is
from the right of the string.

Here is my solution:

set decimalPlaces to 0
set x to (the text returned of (display dialog "Number:" default answer
""))
if x contains "." then set decimalPlaces to (offset of "." in ((reverse of
(characters of x)) as string)) - 1
display dialog "The number of decimal places of " & x & " is " &
decimalPlaces

The AppleScript Text Item Delimiter is a good solution but as it stands
does not take into consideration the possibility of a whole number. Looking
forward to seeing other solutions.

Shirley Hopkins
FaceSpan Support
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: Number of Decimal Places
  • Next by Date: Re: Number of Decimal Places
  • Previous by thread: Re: Number of Decimal Places
  • Next by thread: Re: Number of Decimal Places
  • Index(es):
    • Date
    • Thread