Re: add_commas()
- Posted by Robert B Pilkington <bpilkington at JUNO.COM> Aug 09, 1997
- 815 views
>> 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.