Re: Crunch numbers
Re: Crunch numbers
- Subject: Re: Crunch numbers
- From: Martin Crisp <email@hidden>
- Date: Thu, 14 Mar 2002 15:27:31 +1100
- Organization: Tesseract Computing
On Wed, 13 Mar 2002 14:57:41 +1100 the muse struck
email@hidden, who wrote (in message
<
email@hidden>):
>
Hi all,
>
>
How do I get the nearest value?
<pedant mode>
Just divide them.
You meant to say "to the nearest 0.1" :-)
</pedant>
>
set x to "2,84"
>
set y to "135,7"
>
y / x
>
>
--Result: 47.781690140845
>
--Crunch this number to the nearest highest value "47,8"
on run
-- example specific stuff
set round_to to 0.1 --any number (sign makes no diff), 0=no rounding
set x to 2.84
set y to 135.7
set z to 0
if x 0 then
set z to y / x
end if
-- more general stuff
set rounded_result to 0
round_it(z, round_to) returning rounded_result
-- display the result
display dialog (y & " divided by " & x & " rounded to nearest " & ,
round_to & "= " & rounded_result) as string
end run
on round_it(anum, round_to)
try
if round_to 0 then
set roundfactor to 1 / round_to
set anum to ,
((anum * roundfactor) round rounding to nearest) /
roundfactor
end if
on error
set anum to 0
end try
return anum
end round_it
continuation character (,) used for email formatting purposes only :-)
if speed is critical you might want to wrap the call to round_it() in an
'if z0 then'
Have Fun
Martin
--
I have absolutely no reason to complain about "success" with my work;
[...] And yet, from time to time a boundless sense of inferiority
plagues me, a desperate feeling of general failure; how does a person
acquire such bits of lunacy?
- MC Escher
Almost always SMASHed
_______________________________________________
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.