1. Euphoria float and format ints

This is a multi-part message in MIME format.

------=_NextPart_000_0007_01C3E411.9B50B300
	charset="iso-8859-1"

Hello all,

I was wondering if this might be of some use in the finance sector.

I call it a Pretty-Print for floats, atoms and integers that is user adjustable.

Attached format.txt file

Special thanks to Tommy Carlier (get well soon) and Allen V. Robnett
for their input and code adjustments.

Euman
------=_NextPart_000_0007_01C3E411.9B50B300
Content-Type: text/plain;
	name="format.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="format.txt"

global function trm_formatInteger(atom number, integer separator, 
integer plus_sign)
	integer piece, sign
	sequence str, format_string
	
	number = floor(number)
	if number > -1000 and number < 1000 then
		if number >= 0 and plus_sign then
			return '+' & sprintf("%d", number)
		else
			return sprintf("%d", number)
		end if
	end if
	
	format_string = separator & "%03d"
	
	sign = (number >= 0) * 2 - 1
	number *= sign
	str = {}
	while number >= 1000 do
		piece = remainder(number, 1000)
		number = floor(number / 1000)
		str = sprintf(format_string, piece) & str
	end while
	str = sprintf("%d", number) & str
	
	if sign = -1 then return '-' & str
	elsif plus_sign then return '+' & str
	else return str
	end if
end function

global function trm_formatAtom(atom number, integer separator, integer 
real_separator, integer decimals, integer plus_sign)
    sequence str
    atom multiplier
    if decimals <= 0 then
        return trm_formatInteger(number + 0.5, separator, plus_sign)
    else str = trm_formatInteger(number + (number<0)*(floor(number)!=number), 
               separator, plus_sign)  --<<==
    end if
    
    if number < 0 then
        number = -number end if
    multiplier = power(10, decimals)
    number = remainder(floor(number * multiplier + 0.5), multiplier)
return str & real_separator & sprintf("%0" & sprintf("%d", decimals) & "d",
    number)
end function

puts(1, trm_formatAtom(1234567890.666665, ',', '.', 5, 0) & '\n')
puts(1, trm_formatAtom(1234567890.666663, ',', '.', 5, 0) & '\n')

puts(1, trm_formatAtom(-1234567890.666665, ',', '.', 5, 0) & '\n')
puts(1, trm_formatAtom(-1234567890.666663, ',', '.', 5, 0) & '\n')

puts(1, trm_formatAtom(-1234567890.055, ',', '.', 2, 0) & '\n')
puts(1, trm_formatAtom(1234567890.004, ',', '.', 2, 0) & '\n')
puts(1, trm_formatAtom(-1234567890.045, ',', '.', 2, 0) & '\n')

if getc( 0 ) then end if
------=_NextPart_000_0007_01C3E411.9B50B300--

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu