PDA

View Full Version : How to edit number precision in Applescript??




crd26
Aug 14, 2006, 11:39 AM
I have been learning Applescript recently and I can't figure out how to format the precision of a number. I have looked through forums and have looked through a couple of books as well as the Applescript Language Guide.

The "round" command comes close but isn't exactly what I need.

What I need in the end it to round numbers to the nearest two decimal places.

example : 18.094898203 to 18.09

Thanks for any help!



Sayer
Aug 14, 2006, 12:23 PM
I have been learning Applescript recently and I can't figure out how to format the precision of a number. I have looked through forums and have looked through a couple of books as well as the Applescript Language Guide.

The "round" command comes close but isn't exactly what I need.

What I need in the end it to round numbers to the nearest two decimal places.

example : 18.094898203 to 18.09

Thanks for any help!

Use this:


(* format is roundThis(inDec, precision) *)

roundThis(75.5436, 1) --this will round to 75.5

on roundThis(n, numDecimals)
set x to 10 ^ numDecimals
(((n * x) + 0.5) div 1) / x
end roundThis

There is this invention called "Google" that actually lets you search for things (http://www.google.com/search?q=applescript+round+number&ie=UTF-8&oe=UTF-8) you are looking for the answers to your question. Trully amazing, really.

crd26
Aug 14, 2006, 12:54 PM
I appreciate your help.

My looking through Google didn't pay off for me. I'm glad it did for you and thank you for sharing your findings.

SC68Cal
Aug 15, 2006, 01:49 PM
Can't you just use a double variable?

Then again, I have not gone that in depth into applescript using numbers, that's where I just fall back to Java.

EDIT: Applescript doesn't support double types. Not suprised, but it's kind of an expected features, you know?