Real to fix point as text

need (“5000000008.1234” as real) --or some real
convert to “5000000008.12”

0 > “0.00”
1 > “1.00”
1.0 >“1.00”
-0.6 >“-0.60”
0.01>“0.01”
-0.001>“0.00”
1.239>“1.23”

i do:
get class of ((“5000000008.1234” as real) as integer) --result Real
get “5000000008.1234” as real --2.147483647E+9


on RealToFixPoint(float)
return ((float_number div 0.01) / 100) as Unicode text
end RealToFixPoint

get RealToFixPoint(some number) --2.147483647E+9
get float as unicode text -all Ok: “12345.1234”

Hello

I was unable to get a correct behavior with your handler so I built my ugly one:

set deci to character 2 of (0.5 as text)

set theNumber to get "5000000008" & deci & "1234" as real

set theFix to my RealToFixPoint(theNumber as text)
--set theFix to my Real2FixPoint(theNumber)


on Real2FixPoint(float_number)
	return ((float_number div 0.01) / 100) as Unicode text
end Real2FixPoint

on RealToFixPoint(theNumber)
	set deci to character 2 of (0.5 as text)
	try
		if theNumber contains "E" then
			set deb to text 1 thru ((offset of "E" in theNumber) - 1) of theNumber
			if deb starts with deci then
				set p1 to ""
			else
				set p1 to text 1 thru ((offset of deci in deb) - 1) of deb
			end if
			set p2 to text (1 + (offset of deci in deb)) thru -1 of deb
			if theNumber contains "+" then
				set mul to (text (1 + (offset of "+" in theNumber)) thru -1 of theNumber) as integer
				if mul < (count of p2) then
					set p1 to p1 & text 1 thru mul of p2
					set p2 to text (mul + 1) thru -1 of p2
					set p2 to text -2 thru -1 of (((("1" & deci & p2) as number) * 100) div 1 as text)
					set theNumber to p1 & deci & p2
				else
					set theNumber to p1 & p2
				end if
			else
				set mul to (text (1 + (offset of "-" in theNumber)) thru -1 of theNumber) as integer
				set p1 to p1 & p2
				repeat mul times
					set p1 to "0" & p1
				end repeat
				set theNumber to (character 1 of p1) & deci & text 2 thru -1 of p1
			end if
		end if
		return theNumber
	on error
		error "the "" & theNumber & "" is not a number !"
	end try
end RealToFixPoint

Yvan KOENIG (from FRANCE dimanche 1 mars 2009 20:39:34)

thanks! :smiley:

Thanks, on the basis of your code I have made the variant:


set float_number to "5000000008.001" as real

--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) & "0"
set float_fractional to (text (offset of "." in float_fractional) thru ((offset of "." in float_fractional) + 2) of float_fractional)

set float_number to (float_number div 1) as text

if float_number contains "." then
	set pset to offset of "." 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 ((last item of float_number) as integer) + 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 ""

	return (float_number as text) & float_fractional
else
	return float_number & float_fractional
end if

--resilt: "5000000008.00"

set float_number to “1234.001” as real
–result"1234.99"
Fractional numbers are artful, probably the method of division of number on a fractional and whole part requires completion!
mashine translate

As requested in the OP, this truncates, it does not round.

set myNumeral to "5000000008.1264"

if myNumeral contains "." then set myNumeral to (myNumeral & "00")

set decimalPoint to offset of "." in (myNumeral & ".00")

set AppleScript's text item delimiters to {}

(characters 1 thru (decimalPoint + 2) of (myNumeral & ".00")) as string
-- "5000000008.12""

set float_number to “1234.001” as real
–result"1234.99"

it is not round
it is resilt of work my algorithm :frowning:

(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)