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