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