working with multi-decimal numbers in AppleScript

Hi Everyone,

I’m trying to figure out an annoying issue I’m having with comparing numbers within an AppleScript. The code I’m actually using is an installer, so for the purposes of this post, I’ll keep it simple. I currently have the following script:

set kmnumber to "20.2.1" as number
if kmnumber is greater than 8 then say "greater"

If I change that number to anything with 2 decimal places, like “20.1” etc, the script works correctly. But ass soon as the third decimal place is added, it no longer recognizes it as a valid number. Honestly, I really only need to compare the first part of the numbers, before the first decimal, but I don’t know how to park that in a simple way with AppleScript.
Any help is greatly appreciated. Thank you to Rae and Mark and all of the people who make this site possible, it’s much appreciated and truly an invaluable resource.


set kmnumber to "20.2.1"
set AppleScript's text item delimiters to "."
set kmnumber to first text item of kmnumber as integer

if kmnumber is greater than 8 then say "greater"

Your problem was that you can only have a single decimal point in a number. Once you have a second point then the only way it can be treated is as text.

For fun… if you know that there are three numeric components —as above— then you could do something like this to get the first two as a real.

set km to "20.2.1"
set AppleScript's text item delimiters to "."

set kmi to text items of km
--> {"20", "2", "1"}

set kmx to reverse of (rest of reverse of kmi)
--> {"20", "2"}

(kmx as text) as number
--> 20.2

On a side note… you may notice that if you don’t include the quotation marks around your number, the first line will generate an error with three components but not with one or two. In a roundabout way, it’s letting you know that there is an issue with your version number.


A simpler approach would be to compare as text, using considering numeric strings so that “20” is considered greater than “8”, which it wouldn’t normally be:

set kmnumber to "20.2.1"
considering numeric strings
	if kmnumber is greater than "8" then say "greater" -- Compare numeric strings rather than numbers.
end considering

Hi Nigel, yes. Thank you so much, that’s exactly what I was looking for; considering numeric strings. I saw from googling the whole reverse numbers thing, but I knew there had to be a simpler way of doing this. Thank you to all of those who replied as well, it’s all helpful information :slight_smile: