Re: Number of Decimal Places
Re: Number of Decimal Places
- Subject: Re: Number of Decimal Places
- From: Deivy Petrescu <email@hidden>
- Date: Mon, 22 Apr 2002 18:34:31 -0400
>
Hi. Here is an open challenge:
>
>
I would like to, in the fewest lines of code, determine the number
>
of decimal places of a "real" named x.
>
For instance, "1" would be 0, "1.1" would be 2, "1.11" would be 3, etc.
>
Using the following guide, I'd like to know who can come up with the
>
fewest lines of code to return the correct number of decimal places:
>
>
--Use this code:
>
set x to (the text returned of (display dialog "Number:" default
>
answer "")) as real
>
--Code
>
display dialog "The number of decimal places of " & x & " is " &
decimalplaces
>
>
Good luck.
>
>
-Goldfish
Well Mr Goldfish, i don't know what you mean by decimal places.
For instance, is 1.123456789 E12 a number with 9 decimal places or not?
Assuming you are interested in dealing with with numbers that are not
so big, here is a "2" liner for you
set x to (the text returned of (display dialog "Number:" default
answer "")) as real
if (offset of "." in (x as text)) 0 then
set decimalplaces to (length of (x as string)) - (offset of "." in (x as
text))
else
set decimalplaces to 0
end if
display dialog "The number of decimal places of " & x & " is " &
decimalplaces
Tested in OS X.
Check to see what offset of a character that is not there returns in
non X versions and mak the change
--
Deivy Petrescu
http://www.dicas.com
_______________________________________________
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.