• 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: Decimal Point Truncations?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Decimal Point Truncations?


  • Subject: Re: Decimal Point Truncations?
  • From: Jolly Roger <email@hidden>
  • Date: Thu, 08 Feb 2001 13:03:14 -0600
  • Replyto: email@hidden

on 2/8/2001 12:08 PM, Gregory Smith (email@hidden) wrote:

> Hello,
>
> I'm having a problem truncating decimals. Can anyone help?
>
> Example:
>
> set diskSize to free space of startup disk
> --> script code to recalculate diskSize variable to Gb <--
> display dialog diskSize
>
> I want to get the disk size and display the result in Gb with one decimal
> place (If possible). Currently, the full code above gets something like
> 54.6464646748783838. I would like it to display 54.6. I've tried rounding
> and it returns the same 54.6464646748783838 for some reason that escapes me.

Well, these handlers are probably in need of optimization to make them
smaller and more efficient; but it's plain vanilla AppleScript, and gets the
job done quite nicely for me.

Note: I used Jedd's cool solution in the FormatNumber handler - it was way
better than my own.

Enjoy...

-- begin script
tell application "Finder" to set diskSize to free space of startup disk
return FormatSize(diskSize, 1)

-------------------------------------------------------

on FormatNumber(num, remainderDigits, leadingDigits, padCharacter)
try
-- make sure we are working with a string
set num to (num as text)

-- truncate if necessary
if (length of num)  ((offset of "." in num) + remainderDigits) then
set answer to text 1 thru ((offset of "." in num) +
remainderDigits) of num
else
set answer to num as text
end if

-- pad if necessary
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set {lefty, righty} to text items of answer
set AppleScript's text item delimiters to oldDelim
if (leadingDigits > 0) and (leadingDigits > the length of lefty)
then
repeat with n from 1 to (leadingDigits - the (length of lefty))
set lefty to padCharacter & lefty as text
end repeat
end if
if (remainderDigits > 0) and (remainderDigits > the length of
righty) then
repeat with n from 1 to (remainderDigits - the (length of
righty))
set righty to righty & padCharacter as text
end repeat
end if
set answer to lefty & "." & righty

return (answer as text)
on error errmsg
log errmsg
return "n/a"
end try
end FormatNumber

-------------------------------------------------------

on FormatSize(bytes, numDigits)
try
if bytes is greater than (1024 * 1024 * 1024) then
set bytes to (bytes / 1024 / 1024 / 1024)
set theLabel to " GB"
else if bytes is greater than (1024 * 1024) then
set bytes to (bytes / 1024 / 1024)
set theLabel to " MB"
else if bytes is greater than 1024 then
set bytes to (bytes / 1024)
set theLabel to " K"
else
set theLabel to " bytes" -- octets French
end if

set theSize to FormatNumber(bytes, numDigits, 0, "0")

set answer to theSize & theLabel
return answer
on error errmsg
log errmsg
return "n/a"
end try
end FormatSize
-- end script

(Watch for line wraps and character translations done by the list server...)

HTH

JR


References: 
 >Decimal Point Truncations? (From: "Gregory Smith" <email@hidden>)

  • Prev by Date: Re: Decimal Point Truncations?
  • Next by Date: Quark & me
  • Previous by thread: Re: Decimal Point Truncations?
  • Next by thread: Re: Decimal Point Truncations?
  • Index(es):
    • Date
    • Thread