Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How does Studio handle rounded numbers?



On 8/9/03 8:00 AM, "Mark Butcher" <email@hidden> wrote:

> Here's a summary of my code in a Studio app:
>
> set p to (round (9.85165247269 * 100) rounding up) / 100
>
> log p
> --> 9.86
>
> set contents of data cell "something" of data row dataRow of dataSource
> to p
> --> 9.86
>
> set contents of text field "whatever" of window "wherever" to p
> --> 9.859999999999999
>
> ???
>
> The log and table correctly display the number (p) to 2 decimal places.
> The text field does not.

It's something to do with how computers handle "floating point" numbers. I
can't recall the details. If this is merely for displaying in a text field,
then just coerce to text before displaying:

set p to ((round (9.85165247269 * 100) rounding up) / 100) as string
--> "9.86"

set contents of data cell "something" of data row dataRow of dataSource
to p
--> should be "9.86"

If you need to actually manipulate the numbers in the table, use a number
formatter to get the two decimal places, not your code above. There are a
few example projects, and also a section of the Tutorial that comes with
Studio, that deal with exactly this.

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

References: 
 >How does Studio handle rounded numbers? (From: Mark Butcher <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.