AppleScript Library

Number Library

   The number library contains some useful handlers for working with numbers such as some basics, rounding, converting to string, maximum/minimum, roman numerals,...

All handlers displayed on this page were freely available on the internet or written by myself. Please refer to the copyright section at the end of this page. Feedback appreciated.


AppleScript Editor open download

Properties
myName
Basics
decPt
isOdd
isEqual
gcd
abs
modf
Rounding
ceil
floor
roundToNearest
roundTowardZero
roundAsInSchool
Minimum / Maximum
minnum
maxnum
Number To String
padNum
numberToString
roundTruncate
Roman Numerals
arabic2roman
roman2arabic

Properties

property myName

Name that should be used when loading this library.

show source AppleScript-Editor open insert append toc
property myName : "_number"

Basics

decPt()

Get the decimal point separator as defined in system preferences.

string"," or "."

Written by:HAS

Example:

decPt() 
--> "," (on my German machine)
show source AppleScript Editor open insert append toc
on decPt()
	return character 2 of (0.0 as string)
end decPt

isOdd(this_number)

Determine whether a passed numeric value is even or odd.

this_numberinteger
boolean

Written by:Mac OS X Automation

Example:

isOdd(5) 
--> true
show source AppleScript Editor open insert append toc
on isOdd(this_number)
	local this_number
	if this_number mod 2 is not 0 then
		return true
	else
		return false
	end if
end isOdd

isEqual(x, y)

Are two numbers equal, allowing for a reasonable margin of error? (Note that '0.2 * 0.2 = 0.04' will return false in AppleScript)

xreal
yreal
boolean

Written by:HAS

Example:

isEqual(0.2 * 0.2, 0.04) 
--> true
show source AppleScript Editor open insert append toc
on isEqual(x, y)
	local x, y, delta
	try
		set delta to 1.0E-9
		return (x - delta < y) and (x + delta > y)
	on error eMsg number eNum
		error "Can't isEqual(x,y): " & eMsg number eNum
	end try
end isEqual

gcd(x, y)

Greatest common divisor of two numbers.

xinteger or real
yinteger or real
integer or real

Written by:ljr

Example:

gcd(27, 21) 
--> 3
show source AppleScript Editor open insert append toc
on gcd(x, y)
	local x, y
	try
		if y = 0 then
			return x
		else
			return my gcd(y, x mod y)
		end if
	on error eMsg number eNum
		error "Can't gcd(x,y): " & eMsg number eNum
	end try
end gcd

abs(x)

Return the absolute value of x.

xany number
numbersame class as input

Written by:HAS

Example:

abs(-5) 
--> 5
show source AppleScript Editor open insert append toc
on abs(x)
	local x
	try
		if x < 0 then
			return -x
		else
			return x
		end if
	on error eMsg number eNum
		error "Can't abs(x): " & eMsg number eNum
	end try
end abs

modf(x)

Return the fractional and integer parts of x. Both results carry the sign of x.

xreal
list

Written by:ljr

Example:

modf(-4.04) 
--> {-0.04,- 4}
show source AppleScript Editor open insert append toc
on modf(x)
	local x
	try
		set x to {x mod 1, x div 1}
		return x
	on error eMsg number eNum
		error "Can't modf(x): " & eMsg number eNum
	end try
end modf

Rounding

ceil(x)

Return the ceiling of x, the smallest integer value greater than or equal to x.

xany number
integer

Written by:ljr

Example:

ceil(-6.35) 
--> -6
show source AppleScript Editor open insert append toc
on ceil(x)
	local x
	try
		return round (x) rounding up
	on error eMsg number eNum
		error "Can't ceil(x): " & eMsg number eNum
	end try
end ceil

floor(x)

Return the floor of x, the largest integer value less than or equal to x.

xany number
integer

Written by:ljr

Example:

floor(-6.35) 
--> -7
show source AppleScript Editor open insert append toc
on floor(x)
	local x
	try
		return round (x) rounding down
	on error eMsg number eNum
		error "Can't floor(x): " & eMsg number eNum
	end try
end floor

roundToNearest(x)

Round fractions < .5 towards zero, fractions = .5 to nearest even number, and fractions > .5 away from zero.

xany number
integer

Written by:ljr

Example:

roundToNearest(2.5) 
--> 2
show source AppleScript Editor open insert append toc
on roundToNearest(x)
	local x
	try
		return (round x)
	on error eMsg number eNum
		error "Can't roundToNearest(x): " & eMsg number eNum
	end try
end roundToNearest

roundTowardZero(x)

Round fractions towards zero.

xany number
integer

Written by:HAS

Example:

roundTowardZero(-2.5) 
--> -2
show source AppleScript Editor open insert append toc
on roundTowardZero(x)
	local x
	try
		return (x div 1)
	on error eMsg number eNum
		error "Can't roundTowardZero(x): " & eMsg number eNum
	end try
end roundTowardZero

roundAsInSchool(num)

Round fractions < .5 towards zero and fractions > or = .5 away from zero.

xreal
integer

Written by:ljr

Example:

roundAsInSchool(2.5) 
--> 3
show source AppleScript Editor open insert append toc
on roundAsInSchool(x)
	local x, vz
	try
		return (round x rounding as taught in school)
	on error eMsg number eNum
		error "Can't roundAsInSchool(x): " & eMsg number eNum
	end try
end roundAsInSchool

Minimum / Maximum

minnum(lst)

Find lowest number in list.

lstlist of numbers
number

Written by:HAS

Example:

minnum({8, 2, 6}) 
--> 2
show source AppleScript Editor open insert append toc
on minnum(lst)
	local lst, l, res, i
	try
		considering hyphens, punctuation and white space
			if lst's class is not list then error "not a list." number -1703
			script k
				property l : lst
			end script
			set res to k's l's item 1 as number
			repeat with i in k's l
				if res > i's contents then set res to i's contents as number
			end repeat
		end considering
		return res
	on error eMsg number eNum
		error "Can't minnum: " & eMsg number eNum
	end try
end minnum

maxnum(lst)

Find highest number in list.

lstlist of numbers
number

Written by:HAS

Example:

maxnum({8, 2, 6}) 
--> 8
show source AppleScript Editor open insert append toc
on maxnum(lst)
	local lst, l, res, i
	try
		considering hyphens, punctuation and white space
			if lst's class is not list then error "not a list." number -1703
			script k
				property l : lst
			end script
			set res to k's l's item 1 as number
			repeat with i in k's l
				if res < i's contents then set res to i's contents as number
			end repeat
		end considering
		return res
	on error eMsg number eNum
		error "Can't maxnum: " & eMsg number eNum
	end try
end maxnum

Number To String

padNum(num, len)

Convert positive integer to string padded with leading zeros

numintegernumber to pad
lenintegerpad length (2-8)
string

Written by:HAS

Example:

padNum(1, 3) 
--> "001"
show source AppleScript Editor open insert append toc
on padNum(num, len)
	local num, len
	try
		set num to num as integer
		set len to len as integer
		if num < 0 then error "number is negative." number -1704
		if len < 2 or len > 8 then ¬
			error "pad length is out of range (2-8)." number -1704
		set num to num as string
		if num's length > len then ¬
			error "number is longer than pad length." number -1704
		return text -len thru -1 of ("00000000" & num)
	on error eMsg number eNum
		error "Can't padNum: " & eMsg number eNum
	end try
end padNum

numberToString(this_number)

Convert number to string (usefull for scientific notation).

this_numberany number
string

Dependencies: decPt

Written by:slightly modified from Mac OS X Automation

Example:

numberToString(1.234E+9) 
--> "1234000000"
show source AppleScript Editor open insert append toc
on numberToString(this_number)
	local this_number, decimal_adjust, first_part, second_part, converted_number
	try
		set this_number to this_number as string
		if this_number contains "E+" then
			set x to offset of my decPt() in this_number
			set y to offset of "+" in this_number
			set z to offset of "E" in this_number
			set decimal_adjust to characters (y - (length of this_number)) thru ¬
				-1 of this_number as string as number
			if x is not 0 then
				set first_part to characters 1 thru (x - 1) of this_number as string
			else
				set first_part to ""
			end if
			set second_part to characters (x + 1) thru (z - 1) of this_number as string
			set converted_number to first_part
			repeat with i from 1 to decimal_adjust
				try
					set converted_number to ¬
						the converted_number & character i of second_part
				on error
					set converted_number to converted_number & "0"
				end try
			end repeat
			return converted_number
		else
			return this_number
		end if
	on error eMsg number eNum
		error "Can't numberToString: " & eMsg number eNum
	end try
end numberToString

roundTruncate(this_number, decimal_places)

Round and truncate a number to the given decimal places.

this_numberany number
decimal_placesinteger
string

Dependencies: decPt, numberToString

Written by:slightly modified from Mac OS X Automation

Example:

roundTruncate(1.2358, 2) 
--> "1,24"
show source AppleScript Editor open insert append toc
on roundTruncate(this_number, decimal_places)
	local this_number, decimal_places, rounding_value
	local mod_value, second_part, first_part, decPt
	try
		set decPt to my decPt()
		if decimal_places is 0 then
			set this_number to this_number + 0.5
			return my numberToString(this_number div 1)
		end if
		set rounding_value to "5"
		repeat decimal_places times
			set rounding_value to "0" & rounding_value
		end repeat
		set rounding_value to (decPt & rounding_value) as number
		set this_number to this_number + rounding_value
		set mod_value to "1"
		repeat decimal_places - 1 times
			set mod_value to "0" & mod_value
		end repeat
		set mod_value to ("," & mod_value) as number
		set second_part to (this_number mod 1) div mod_value
		if length of (second_part as text) is less than decimal_places then
			repeat decimal_places - (length of (second_part as text)) times
				set second_part to ("0" & second_part) as string
			end repeat
		end if
		set first_part to this_number div 1
		set first_part to my numberToString(first_part)
		set this_number to (first_part & decPt & second_part)
		return this_number
	on error eMsg number eNum
		error "Can't round_truncate: " & eMsg number eNum
	end try
end roundTruncate

Roman Numerals

arabic2roman(n)

Convert arabic to roman numerals.

ninteger or string
string

Written by:Piero Garzotto

Example:

arabic2roman("1989") 
--> "MCMLXXXIX"
show source AppleScript Editor open insert append toc
on arabic2roman(n)
	local r, i, n
	try
		if (n as integer) > 3999 then error "Max number is 3999" number 1
		set r to ""
		repeat with i from 1 to (count (n as string))
			set r to item (((item -i of (n as string)) as integer) + 1) of item i of ¬
				{{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}, ¬
					{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}, ¬
					{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}, ¬
					{"", "M", "MM", "MMM"}} & r
		end repeat
		return r
	on error eMsg number eNum
		error "Can't arabic2roman: " & eMsg number eNum
	end try
end arabic2roman

roman2arabic(str)

Convert roman to arabic numerals.

strstring
integer

Written by:Piero Garzotto

Example:

roman2arabic("MCMLXXXIX") 
--> 1989
show source AppleScript Editor open insert append toc
on roman2arabic(str)
	local str, r, i
	set r to 0
	repeat with i from 1 to count str
		set r to r + (item (offset of (item i of str) in "mdclxvi") of ¬
			{1000, 500, 100, 50, 10, 5, 1}) * (((((offset of ¬
			(item (i + 1) of (str & "@")) in "mdclxvi@") ≥ (offset of ¬
			(item i of str) in "mdclxvi")) as integer) * 2) - 1)
	end repeat
	return r
end roman2arabic

Copyright

© 2003 HAS (http://applemods.sourceforge.net)
decPt, isEqual, abs, roundTowardZero, minnum, maxnum, padNum
© 2008 ljr (http://applescript.bratis-lover.net)
gcd, modf, ceil, floor, roundToNearest, roundAsInSchool

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


© 2008 Mac OS X Automation (http://www.macosxautomation.com)
numberToString, roundTruncate
© 2008 Piero Garzotto (http://scriptbuilders.net/files/romannumerals1.0.html)
arabic2roman, roman2arabic

Please refer to the authors' websites for copyright information.