string to number decimal isssue

Hi,

Consider:

set num to "642.284057617188" as real --> fails

set num to "642,284057617188" as real --> works

…at least on my machine. I read somewhere that applescript numbers take dots not comma’s, so it took me a while to figure out what went wrong.

Is anyone running into the same issue? Is this due to systemwide international settings that influence this? Or is it an internal applescript setting which I can change in the script itself?

(I was trying to read the size of a PDF, and the shell returns numbers from the metadata with dots)

Yes. The decimal points in AppleScript reals are represented as dots, but AppleScript refers to the user’s Language & Text preferences when creating or interpreting text representations of reals.


set num to "642.284057617188"

set o to (offset of "." in num)
if ((o > 0) and (0.0 as text is "0,0")) then set num to (text 1 thru (o - 1) of num & "," & text (o + 1) thru -1 of num)
set num to num as integer

Ah I thought as much.

if (0.0 as text is "0,0")

Nice clever test! Thanks for the info, one less “weird” problem to deal with :slight_smile:

Caution.

The described test isn’t sufficient.

AppleScript doesn’t accept the thousands separators.
I received these strings:

“1’234’567,8901”
“1 234 567,8901”
“1.234.567,8901”
“1,234,567.8901”

All of them are valid in the country in which they where issued.
This why I use the pasted piece of code to normalize them in the format used locally.


on run
	set valeurs_initiales to "1'234,56
234,68
1.234,567
+1 234,567
12A3c56
1,234.5678
1,234,567.89
-1.2345678E+5"
	
	set en_liste to paragraphs of valeurs_initiales
	set |normalisés| to {}
	set bizarre to {}
	
	repeat with ref_numero in en_liste
		set {un_nombre, un_flag} to my stringToNumber(ref_numero as text)
		if un_flag then
			copy un_nombre to end of |normalisés|
		else
			copy un_nombre to end of bizarre
		end if
	end repeat
	
	|normalisés| & return & "bizarre" & return & bizarre
end run

--=====

on stringToNumber(s)
	try
		s as number
	on error
		if 5 > (system attribute "sys2") then
			set |insécable| to ASCII character 202
		else
			set |insécable| to character id 160
		end if
		
		set |autorisés| to characters of "123456789,.+-'E" & space & |insécable|
		set chars to characters of s
		set est_un_nombre to true
		repeat with c in chars
			if c is not in |autorisés| then
				set est_un_nombre to false
				exit repeat
			end if
		end repeat
		if not est_un_nombre then return {s, false}
		set |déci_local| to character 2 of (0.5 as text)
		if |déci_local| is "," then
			set |déci_étranger| to "."
		else
			set |déci_étranger| to ","
		end if
		(*
remove possible thousands separators
*)
		if (s contains |déci_local|) and (s contains |déci_étranger|) then
			if (offset of |déci_étranger| in s) > (offset of |déci_local| in s) then
				set s to my remplace(s, |déci_local|, "")
			else
				set s to my remplace(s, |déci_étranger|, "")
			end if
		end if
		(*
Replace wrong separator by the local one
*)
		if s contains space then set s to my supprime(s, space)
		if s contains "'" then set s to my supprime(s, "'")
		if s contains |insécable| then set s to my supprime(s, |insécable|)
		if s contains |déci_étranger| then set s to my remplace(s, |déci_étranger|, |déci_local|)
		if s starts with "+" then set s to text 2 thru -1 of s
	end try
	return {s, true}
end stringToNumber

--=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
	local oTIDs, l
	set oTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to d1
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

--=====
(*
removes every occurences of d in the text t
*)
on supprime(t, d)
	local oTIDs, l
	set oTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to d
	set l to text items of t
	set AppleScript's text item delimiters to ""
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end supprime

--=====

Yvan KOENIG (VALLAURIS, France) jeudi 12 août 2010 21:02:37