• 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: How do I format a number or Round a number?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How do I format a number or Round a number?


  • Subject: Re: How do I format a number or Round a number?
  • From: Walter Ian Kaye <email@hidden>
  • Date: Fri, 10 Oct 2003 14:07:27 -0700

At 12:20p -0500 10/10/2003, Steve Mills didst inscribe upon an electronic papyrus:

On Friday, Oct 10, 2003, at 12:10 US/Central, Steve Mills wrote:

On Friday, Oct 10, 2003, at 11:49 US/Central, Work wrote:

I am trying to get Applescript to get the size of my Hard drive in a
display dialog. I use the get Free Space command and then divide by
1024/1024/1024 to get Gigabytes. The result is correct however the
number displays as "1.571773529053" I would like to display as a
rounded number. Maybe "1.57GB" Any help would be greatly appreciated.

(round (1.571773529053 * 100)) / 100

Or skip a few calculations:

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". What you'll need are threshholds(sp?) so that e.g. 1024MB kicks up to 1GB. Below is some of my old Perl code, showing a threshhold of 1048064 rather than 1048576 for going from KB to MB. I haven't tested for GB threshhold since I wrote this code for file sizes rather than disk sizes, but I leave that as an exercise for the reader (along with porting to AS, although now i feel tempted...).


# convert bytes to "nnn KB"/"nnn MB":
sub KiloMega {
local($bytes) = @_;
local($out);

if ($bytes >= 1048064) {
$bytes = &TriDigits($bytes/1048576);
$out = "$bytes MB";
}
elsif ($bytes >= 1024) {
$bytes = &TriDigits($bytes/1024);
$out = "$bytes KB";
}
else {
$out = "$bytes bytes";
}
}


sub TriDigits {
local($num) = @_;
local($out);

if ($num >= 100) { # if 100.0-9999.99, round to 0 dec places
$out = &RoundNum($num);
}
elsif ($num >= 10) { # if 10.0-99.9, round to 1 dec place
$out = &RoundNum($num,-1);
}
else { # if 0.00-9.99, round to 2 dec places
$out = &RoundNum($num,-2);
}
}


sub RoundNum {
local($num,$places) = @_;
local($isneg,$whole,$part,$mult,$out,$adj);
$places = 0 unless $places;
$mult = 10**$places;
$num /= $mult;
if ($num < 0) {
$num = 0 - $num;
$isneg = 1;
}
else { $isneg = 0; }
$whole = int($num);
$part = $num - $whole;
$out = ($part < 0.5) ? $whole : $whole + 1 ;
$out *= $mult;
if ($places < 0) {
$dpos = index($out, ".");
unless ($dpos < 0) {
$adj = 10**($places-1);
$out += $adj;
$out = substr($out,0,$dpos+1-$places);
}
}
$out = ($isneg) ? 0 - $out : $out ;
}
_______________________________________________
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.

References: 
 >Re: How do I format a number or Round a number? (From: Steve Mills <email@hidden>)

  • Prev by Date: Re: Call an osax remotely?
  • Next by Date: Re: application file of remote app broken
  • Previous by thread: Re: How do I format a number or Round a number?
  • Next by thread: Re: How do I format a number or Round a number?
  • Index(es):
    • Date
    • Thread