Re: How do I format a number or Round a number?
Re: How do I format a number or Round a number?
- Subject: Re: How do I format a number or Round a number?
- From: Nigel Garvey <email@hidden>
- Date: Sun, 12 Oct 2003 14:06:46 +0100
John Cochrane wrote on Sun, 12 Oct 2003 14:24:59 +1000:
>
The above is probably cleaner and more accurate than than code below
>
that I have been using to get the size to 1 decimal place
Hi, John. Your code seems mostly fine. I hope you won't mind me
mentioning a few things.
>
on roundsize(_bytes)
>
if _bytes > 1.073741824E+9 then
>
set _bytes to _bytes / 1.073741824E+9
>
set _units to "GB"
You'll also want to set these "GB" values when _bytes is *exactly*
1.073741824E+9, so the 'if' line should ideally be:
if _bytes >= 1.073741824E+9 then
>
else if _bytes < 1.073741824E+9 and _bytes > 1048576 then
This line will only be executed if _bytes was found not to be greater
than or equal to 1.073741824E+9 by the previous 'if' line. _bytes *must*
therefore be less than that amount and there's no need to test this
again. As above, you'll want to include when _bytes is exactly 1048576:
else if _bytes >= 1048576
>
else if _bytes < 1048576 and _bytes > 1024 then
Similarly:
else if _bytes >= 1024
>
else
>
set _bytes to _bytes / 1
<!> Perhaps you mean '_bytes div 1' or '_bytes as integer'.
>
set sizeValue to (round (_bytes * 10)) / 10
As I hinted in my previous post, 'round' will round to nearest (IEEE
style), which may be up or down. That's absolutely fine if it's what you
want. But if you want to imitate the values given in the Finder
information window displays, you have to round down. You could do this
with the 'rounding down' parameter:
set sizeValue to (round (_bytes * 10) rounding down) / 10
... but it's simpler and very much faster to use vanilla AppleScript
maths:
set sizeValue to _bytes * 10 div 1 / 10
Multiplying by ten and divving the result by 1 (to lose the fractional
part of the result) can be combined into one action - divving by 0.1:
set sizeValue to _bytes div 0.1 / 10
(The result is a real, even if _bytes was an integer to start with.)
>
However I find that they both have problems if used to get the size of
>
a folder.
>
The first time that the scripts are run they return a missing value. If
>
repeated they return a result but this does not agree with the size
>
given in the folders "Info" window
I can duplicate the 'missing value' effect in Mac OS X immediately after
logging in. Forcing the Finder to 'update' the folder before getting its
size seems to do the trick.
The 'Size:' figures in the information window for a folder show its
'physical size' (the amount of disk space devoted to the folder and its
contents), followed in parentheses by its 'size' (the combined sizes of
the individual items). The 'physical size' will usually be a multiple of
4 KB. If you apply your handler to this, you should get a similar result
to that shown in the information window.
set theFolder to (choose folder)
tell application "Finder"
update theFolder with necessity
repeat
set {_physicalSize, _size} to theFolder's {physical size, size}
if the result does not contain missing value then exit repeat
end
end tell
set _size to _size div 1
roundsize(_physicalSize)
set _physicalSize to (item 1 of the result) as string & " " & item 2 of
the result
set msg to "Size: " & _physicalSize & " (" & _size & " bytes)"
display dialog msg
NG
_______________________________________________
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.