• 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: rounding to decimal place
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: rounding to decimal place


  • Subject: Re: rounding to decimal place
  • From: Stefan Eriksson <email@hidden>
  • Date: Sun, 9 Oct 2005 21:21:33 +0200

what is the best way to round numbers to the nearest decimal place?

1.123945565
to
1.124


The classic procedure is:

1) multiply by 1000
2) add 0.5
3) truncate to an integer
4) divide by 1000

But step 4 may introduce an "error" because 1/1000 is not exactly expressible as a floating point binary number. Figuring out how to tell AppleScript to print in a three decimal digit format is beyond my mental capacity. Perhaps inserting the decimal point into the character string that represents the integer at step 3.

Maybe this will help ?

Not my code so don´t thank me...

/ S

roundThis(75.5436123, 2) --> 75.54 

on roundThis(n, numDecimals)
    set x to 10 ^ numDecimals
    (((n * x) + 0.5) div 1) / x
end roundThis
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Re: rounding to decimal place
  • Next by Date: Re: recursion depth
  • Previous by thread: Re: rounding to decimal place
  • Next by thread: Re: rounding to decimal place
  • Index(es):
    • Date
    • Thread