1. add_commas()

Okay... don't worry. This is the last one right now... :)
Ever notice that there are no commas in the numbers outputed in Euphoria?
Qbasic has a PRINT USING that you can use to add commas... Well, here is
a Euphoria routine to make your output a little nicer... Feel free to
improve on it.
So far, you can't set the resolution of the decimals (Like 3,400.00 can't
be done easily, it would be 3,400. So feel free to fix that... :)

global function add_commas(atom number)
-- Adds commas to numbers to make them more readable
sequence revised
atom float, continue
continue = 0
float = 0
revised = sprintf("%f", {number})
while continue = 0 do
    if revised[length(revised)] = '0' then
        revised = revised[1..(length(revised)-1)]
    else
        if revised[length(revised)] = '.' then
            revised = revised[1..(length(revised)-1)]
        end if
        continue = 1
    end if
end while
float = find('.', revised)
if float != 0 then
    for j = (float-4) to 1 by -3 do
        revised = revised[1..j] & ',' & revised[j+1..length(revised)]
    end for
else
    if length(revised) > 3 then
        for j = (length(revised)-3) to 1 by -3 do
            revised = revised[1..j] & ',' & revised[j+1..length(revised)]
        end for
    end if
end if
return(revised)
end function

new topic     » topic index » view message » categorize

2. Re: add_commas()

> Ever notice that there are no commas in the numbers outputed in Euphoria?
>  Qbasic has a PRINT USING that you can use to add commas... Well, here is
>  a Euphoria routine to make your output a little nicer... Feel free to
>  improve on it.
>  So far, you can't set the resolution of the decimals (Like 3,400.00 can't
>  be done easily, it would be 3,400. So feel free to fix that... :)

Here's my 2 cents worth.

Steve Elder

-- code begins here
sequence number2,b,c
atom control, controlb
number2 = ""
b = ""
c = ""
control = 1
controlb = 0

global function add_commas(sequence a)

    -- read input from right to left
    for i = length(a) to 1 by - 1 do
        if a[i] = '.' then
            control = 2
        end if

        -- copy digits right of decimal point with no change
        if control = 1 then
            b = b & a[i]
        -- put in first comma left of decimal point
        elsif control = 2 then
            b = b & a[i]
            controlb = controlb + 1
            if controlb = 4 then
                b = b & ','
                control = 3
                controlb = 0
            end if
        -- put in the rest of the commas
        elsif control = 3 then
            b = b & a[i]
            controlb = controlb + 1
            if controlb = 3 then
                b = b & ','
                control = 3
                controlb = 0
            end if
        end if
    end for

    -- reverse it so the numbers are in the correct order
    for ii = length(b) to 1 by - 1 do
        c = c & b[ii]
    end for
    return c
end function

-- usage example
sequence g
g = add_commas("1123456789.123456")
puts(1,g)

new topic     » goto parent     » topic index » view message » categorize

3. Re: add_commas()

>> Ever notice that there are no commas in the numbers outputed in
Euphoria?
>>  Qbasic has a PRINT USING that you can use to add commas... Well, here
is
>>  a Euphoria routine to make your output a little nicer... Feel free to
>>  improve on it.
>>  So far, you can't set the resolution of the decimals (Like 3,400.00
can't
>>  be done easily, it would be 3,400. So feel free to fix that... :)
>
>Here's my 2 cents worth.
>
>Steve Elder
>
>global function add_commas(sequence a)

deleted some code....

>g = add_commas("1123456789.123456")

Mine will do that... Except it accepts numbers. Is what I mean is that it
can't show 304,221,321.10. It will show 304,221,321.1 instead... Which
can make it a little harder to show money. ($33.5 doesn't look right...
;)
But it can be fixed... Here's an updated version... I didn't use the
width field at first because then I'd need to input the width... Anyway,
just use the following example:

global function add_commas(atom number, sequence field)
-- Adds commas to numbers to make them more readable
sequence revised
atom float, continue
continue = 0
float = 0
revised = sprintf("%" & field & "f", {number})
float = find('.', revised)
if float != 0 then
    for j = (float-4) to 1 by -3 do
        revised = revised[1..j] & ',' & revised[j+1..length(revised)]
    end for
else
    if length(revised) > 3 then
        for j = (length(revised)-3) to 1 by -3 do
            revised = revised[1..j] & ',' & revised[j+1..length(revised)]
        end for
    end if
end if
return(revised)
end function

puts(1, add_commas(1234567.890, ".1")) -- Displays 1,234,567.8
puts(1, "\n" & add_commas(12345.5, ".2")) -- Displays 12,345.50
The field sequence is simply the field widths.. You can use "3.4" or
whatever.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu