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: Sat, 11 Oct 2003 15:18:54 +0100
Walter Ian Kaye wrote on Fri, 10 Oct 2003 14:07:27 -0700:
>
At 12:20p -0500 10/10/2003, Steve Mills didst inscribe upon an
>
electronic papyrus:
>
>tell application "Finder"
>
> (round ((free space of disk "Gort") / 1.073742E+7)) / 100
>
>end tell
>
>
The deficiency with using round "raw" like this is that when you're
>
talking KB/MB/GB, you may end up with "1024 MB" when you really want
>
"1 GB".
Richard Morton wrote on Sat, 11 Oct 2003 11:30:54 +1000:
>
-- convertByteSize -- by Nigel Garvey & Richard Morton, 2002 --
>
-- Convert bytes to a readable string in bytes-K-MB-GB as required.
>
-- Pass the number in bytes. Returns string.
>
to convertByteSize on theRawSize
>
set oneK to 2 ^ 10
>
set oneMB to 2 ^ 20
>
set oneGB to 2 ^ 30
>
tell theRawSize to if it >= oneGB then
>
return ((it div oneGB) & "." & text 2 thru 3 of ((100 + ((it mod
>
oneGB) div (oneK * 1.0E+4))) as string) & " GB") as string
>
else if it >= oneMB then
>
return ((it div oneMB) & "." & text 2 thru 3 of ((100 + ((it mod
>
oneMB) div (oneK * 10))) as string) & " MB") as string
>
else if it >= oneK then
>
return (it div oneK & " KB") as string
>
else
>
return (it & " bytes") as string
>
end if
>
end convertByteSize
Two observations. Firstly, in Finder information windows, GB and MB sizes
are rounded *down* to the two decimal places. Secondly, although I had a
hand in writing the above handler, I have to admit that it doesn't
produce the correct results for GB's and MB's. :-\
If you want to mimic the current Finder information window
interpretations:
to convertByteSize from theRawSize
set oneK to 2 ^ 10
set oneMB to 2 ^ 20
set oneGB to 2 ^ 30
if theRawSize >= oneGB then
((theRawSize / oneGB * ((10 ^ 0.5) ^ 2) div 0.1 / 100) as string) &
" GB"
else if theRawSize >= oneMB then
((theRawSize / oneMB * ((10 ^ 0.5) ^ 2) div 0.1 / 100) as string) &
" MB"
else if theRawSize >= oneK then
((theRawSize div oneK) as string) & " KB"
else
return (theRawSize as string) & " bytes"
end if
end convertByteSize
tell application "Finder" to get capacity of startup disk
convertByteSize from result
The mathematical circumlocutions in the GB and MB lines are to shake out
some rare but possible floating-point errors.
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.