Hello Nigel
You are really optimistic.
At least once a week, I receive datas using the decimal period on my machines using the decimal comma.
It’s why I am so involved in this problem.
Here is a handler which works fine on my system using decimal comma.
set someValues to {"1,234.567", "1,234.567,8", "1'234,567'8", "1 234,567 8", "1.234.567", "1,234,567", "1 234 567,89", "12.34", "1,234", "1.234", "1234", "1.234.567.890,123", "1,234,567,890.123"}
set someCleaned to {}
repeat with aValue in someValues
set end of someCleaned to my localizeThisValue(aValue)
end repeat
log someCleaned
# On my system using decimal comma, I get :
-->{1234,567, 1234,5678, 1234,5678, 1234,5678, 1234567, 1234567, 1234567,89, 12,34, ?1,234?, ?1.234?, 1234, 1234567890,123, 1234567890,123}
# On a system using decimal period, It's supposed to return :
-->{1234.567, 1234.5678, 1234.5678, 1234.5678, 1234567, 1234567, 1234567.89, 12.34, ?1,234?, ?1.234?, 1234, 1234567890.123, 1234567890.123}
on localizeThisValue(aValue)
set digits to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
set localDeci to character 2 of (0.5 as text)
repeat while aValue starts with space
if aValue is space then
set aValue to ""
else
set aValue to text 2 thru -1 of aValue
end if
end repeat
repeat while aValue ends with space
set aValue to text 1 thru -2 of aValue
end repeat
set {delim1, off1} to {{}, {}}
repeat with i from 1 to count aValue
set aChar to text item i of aValue
if aChar is not in digits then
set end of delim1 to aChar
set end of off1 to i
exit repeat
end if
end repeat
set {delim2, off2} to {{}, {}}
if delim1 is not {} then
repeat with i from off1 to count aValue by 4
if (i + 4) > (count aValue) then exit repeat
set aChar to text item (i + 4) of aValue
if aChar is not in digits then
if aChar = (item 1 of delim1) then
set end of off1 to (i + 4)
else
set end of delim2 to aChar
set end of off2 to (i + 4)
end if # aChar = delim1
end if # achar is not in digits
end repeat
end if # delim1 is not {}
log ">>>> " & aValue
log delim1
log off1
log delim2
log off2
if delim1 is not {} then
set maybe1 to item 1 of delim1
if delim2 is {} then
set maybe2 to ""
else
set maybe2 to item 1 of delim2
end if
if maybe1 is in {",", "."} and maybe2 is not in {",", "."} then
if ((count off1) = 1) then
if (count aValue) - (item 1 of off1) = 3 then
log "case 00"
# Can't decide if it's the real vw.xyz or the integer vwxyz
set aValue to "?" & aValue & "?"
else
log "case 0"
if maybe2 is not "" then set aValue to my supprime(aValue, maybe2)
set aValue to my remplace(aValue, maybe1, localDeci)
end if
else
log "case 1"
# no decimal separator
set aValue to my supprime(aValue, maybe1)
set aValue to my supprime(aValue, maybe2)
end if
else if maybe1 = "," then
if maybe2 = "." then
log "case 2"
# xx,xxx.xxx is illegal with decimal comma
# so "." is used as decimal separator
set aValue to my supprime(aValue, maybe1)
set aValue to my remplace(aValue, maybe2, localDeci)
else
log "case 3"
# "," is used as decimal separator
set aValue to my supprime(aValue, maybe2)
set aValue to my remplace(aValue, maybe1, localDeci)
end if # if maybe2 is "."
else if maybe1 = "." then
if maybe2 = "," then
if (count off1) > 1 then
log "case 4.1"
# officially, xx.xxx.xxx,xxx is illegal with decimal comma but when it's used, we may decipher it
# so "." is used as decimal separator
set aValue to my supprime(aValue, ".")
set aValue to my remplace(aValue, ",", localDeci)
else
log "case 4.2"
# officially, xx.xxx,xxx is illegal with decimal comma
# so "." is used as decimal separator
set aValue to my supprime(aValue, ",")
set aValue to my remplace(aValue, ".", localDeci)
end if
else
log "case 5"
# "." is used as decimal separator
set aValue to my supprime(aValue, maybe2)
set aValue to my remplace(aValue, maybe1, localDeci)
end if # if delim2 is "."
else
log "case 6"
# maybe1 is a thousand separator, drop it
set aValue to my supprime(aValue, maybe1)
set aValue to my remplace(aValue, maybe2, localDeci)
end if
else
log "case 7"
end if
return aValue
end localizeThisValue
--=====
(*
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
set AppleScript's text item delimiters to oTIDs
return t
end remplace
--=====
(*
removes every occurences of d in 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
--=====
There is just a case (00) which can’t decide what is the real value.
I never ran it on a system with decimal period.
Yvan KOENIG (VALLAURIS, France) dimanche 7 octobre 2012 17:13:49