(a) All your proposals are period_centric.
When used in comma_separator countries the fail.
(b) given point (a) and the need to round, I edited the first part as:
set deci to character 2 of (0.5 as text)
set the_string to “50000000081,0”
if the_string contains “.” and deci is “,” then
set the_string to my remplace(the_string, “.”, “,”)
else if the_string contains “,” and deci is “.” then
set the_string to my remplace(the_string, “,”, “.”)
end if
set float_number to the_string as real
log float_number
–we Will divide number on fractional and I kiss parts as the fractional part in representation “E+” is very difficult for analysis.
set float_fractional to (float_number mod 1) as text
set float_fractional to ((round (float_fractional * 100)) / 100 as text) & “00”
set float_fractional to (text (offset of deci in float_fractional) thru ((offset of deci in float_fractional) + 2) of float_fractional)
set float_number to (float_number div 1) as text
if float_number contains deci then
set pset to offset of deci in float_number
set float_number to characters of float_number
set item pset of float_number to “”
(c) and we fall on this instruction:
set add_zero to ((last item of float_number) as integer) + 3 + pset - (count of float_number)
which is a perfect wrongdoer.
It fails if the int part of the number has 11 digits which gives such a real : 5.0000000081E+10
This is why I edited the script this way:
set deci to character 2 of (0.5 as text)
set the_string to "50000000081,0"
if the_string contains "." and deci is "," then
set the_string to my remplace(the_string, ".", ",")
else if the_string contains "," and deci is "." then
set the_string to my remplace(the_string, ",", ".")
end if
set float_number to the_string as real
set expo to (text (2 + (offset of "E" in float_number as text)) thru -1 of (float_number as text)) as integer
--we Will divide number on fractional and I kiss parts as the fractional part in representation "E+" is very difficult for analysis.
set float_fractional to (float_number mod 1) as text
set float_fractional to ((round (float_fractional * 100)) / 100 as text) & "00"
set float_fractional to (text (offset of deci in float_fractional) thru ((offset of deci in float_fractional) + 2) of float_fractional)
set float_number to (float_number div 1) as text
if float_number contains deci then
set pset to offset of deci in float_number
set float_number to characters of float_number
set item pset of float_number to ""
--The whole part of number guarantees that after "E" there will be plus
-- Quantity of signs, after a point and number after "E+" - will neutralise.
set add_zero to expo + 3 + pset - (count of float_number)
repeat add_zero times
set float_fractional to "0" & float_fractional
end repeat
set item -1 of float_number to ""
set item -2 of float_number to ""
set item -3 of float_number to ""
if expo > 9 then set item -4 of float_number to ""
return (float_number as text) & float_fractional
else
return float_number & float_fractional
end if
on remplace(t, d1, d2)
local l
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 ""
return t
end remplace
Yvan KOENIG (from FRANCE mardi 3 mars 2009 11:42:17)