1. Formating numbers using commas?
- Posted by Sim <starion5wolf at hotmail.com> Jun 29, 2002
- 446 views
Hi, I'm trying to create a small calculator for Win32, but I can't format numbers correctly. Is there a way to format an number / atom using printf() or sprintf() ? I can use this code to write the number in decimals, but I don't see any commas: sprintf("%d", 1234567). I'd like to see: 1,234,567 I tried changing the formating numbers to %0.2d, but that didn't work. I tried looking through the Euphoria documentation, Win32 Demos and other programs, but I can't find any answers. Any ideas or tips would be appreciated. Thanks!
2. Re: Formating numbers using commas?
- Posted by Dan Moyer <DANIELMOYER at prodigy.net> Jun 29, 2002
- 445 views
Sim, Here's something I made once: -- takes an integer and makes a sequence with commas every 3 digits: function PrettyNumbers(object aNumber) sequence PrettyNumber, Temp integer TriCounter PrettyNumber = {} Temp = {} TriCounter = 0 Temp = sprint(aNumber) for n = length(Temp) to 1 by -1 do PrettyNumber = prepend(PrettyNumber,Temp[n]) TriCounter += 1 if TriCounter = 3 and n != 1 then PrettyNumber = prepend(PrettyNumber,',') end if if TriCounter = 3 then TriCounter = 0 end if end for return PrettyNumber end function -------------------------------- ----- Original Message ----- From: "Sim" <starion5wolf at hotmail.com> To: "EUforum" <EUforum at topica.com> Sent: Saturday, June 29, 2002 10:25 PM Subject: Formating numbers using commas? > > Hi, > I'm trying to create a small calculator for Win32, but I can't > format numbers correctly. Is there a way to format an number / atom > using printf() or sprintf() ? I can use this code to write the number in > decimals, but I don't see any commas: sprintf("%d", 1234567). I'd like > to see: 1,234,567 I tried changing the formating numbers to %0.2d, but > that didn't work. I tried looking through the Euphoria documentation, > Win32 Demos and other programs, but I can't find any answers. > > > Any ideas or tips would be appreciated. > > Thanks! > > > >
3. Re: Formating numbers using commas?
- Posted by Kat <gertie at PELL.NET> Jun 30, 2002
- 418 views
On 30 Jun 2002, at 5:25, Sim wrote: > > Hi, > I'm trying to create a small calculator for Win32, but I can't > format numbers correctly. Is there a way to format an number / atom > using printf() or sprintf() ? I can use this code to write the number in > decimals, but I don't see any commas: sprintf("%d", 1234567). I'd like > to see: 1,234,567 I tried changing the formating numbers to %0.2d, but > that didn't work. I tried looking through the Euphoria documentation, > Win32 Demos and other programs, but I can't find any answers. Here is mine. It breaks off the fractional part, and doesn't insert commas in it, but does return it. Kat ---------------------------------------- global function commatize(sequence number) sequence result, deci,int result = "" if match(".",number) then int = number[1..match(".",number)-1] deci = number[match(".",number)+1..length(number)] else int = number deci = "" end if while length(int) do if length(int) >= 3 then result = int[length(int)-2..length(int)] & "," & result int = int[1..length(int)-3] else result = int & "," & result int = "" end if end while result = result[1..length(result)-1] result = result & "." & deci if result[1] = ',' then result = result[2..length(result)] end if return result end function -- commatize(sequence number) -----------------------
4. Re: Formating numbers using commas?
- Posted by Juergen Luethje <jluethje at gmx.de> Jun 30, 2002
- 408 views
Hi Dan, you wrote: [function PrettyNumbers(object aNumber)] Very nice function! I changed it somewhat, so that the principle can be applied more general. Best regards, Juergen ------------------------------------------------------------------->8--- function block_fmt (sequence s, integer delim_char, integer block_len) sequence ret integer counter ret = {} counter = 0 for p = length(s) to 2 by -1 do ret = prepend(ret, s[p]) counter += 1 if counter = block_len then ret = prepend(ret, delim_char) counter = 0 end if end for return prepend(ret, s[1]) end function ------------------------------ include misc.e include machine.e function PrettyNumbers (integer i) return block_fmt(sprint(i), ',', 3) end function function nibbles (integer i) sequence ret integer p ret = reverse(int_to_bits(i, 31)) + '0' p = find('1', ret) if p = 0 then return "0" end if return block_fmt(ret[p..length(ret)], ' ', 4) end function ------------------------------ puts(1, PrettyNumbers(1234) & '\n') puts(1, nibbles(20))
5. Re: Formating numbers using commas?
- Posted by Dan Moyer <DANIELMOYER at prodigy.net> Jun 30, 2002
- 427 views
Sim, And in case you need it, here's a slight rework of "PrettyNumbers" that allows you to send it numbers with a decimal part also. The only thing I can see that you *can't* do with it is send it a number which just ends in a decimal point with no digits to the right, like "1234." (that will *fail*); and it does lose any "precision" that might be implied by "123.000", because it removes the extra zeros (not on purpose, it just does). Maybe someone could make it retain them? Looks like that might happen when I "sprint(aNumber)"? Dan Moyer --<code begins> include misc.e -- takes an number and makes a sequence with commas every 3 digits: function PrettyNumbers(object aNumber) sequence PrettyNumber, Temp, decPart integer TriCounter, decPos PrettyNumber = {} Temp = {} decPart = {} decPos = 0 TriCounter = 0 Temp = sprint(aNumber) decPos = find('.', Temp) if decPos then decPart = Temp[decPos..length(Temp)] Temp = Temp[1..decPos -1] end if for n = length(Temp) to 1 by -1 do PrettyNumber = prepend(PrettyNumber,Temp[n]) TriCounter += 1 if TriCounter = 3 and n != 1 then PrettyNumber = prepend(PrettyNumber,',') end if if TriCounter = 3 then TriCounter = 0 end if end for if not atom(decPart) then PrettyNumber = PrettyNumber & decPart end if return PrettyNumber end function puts(1, PrettyNumbers(123) & "\n") puts(1, PrettyNumbers(123.000) & "\n") puts(1, PrettyNumbers(123.45678) & "\n") puts(1, PrettyNumbers(12345678) & "\n") puts(1, PrettyNumbers(1234.5678) & "\n") puts(1, PrettyNumbers(.12345678) & "\n") --<code ends> ----- Original Message ----- From: "Sim" <starion5wolf at hotmail.com> To: "EUforum" <EUforum at topica.com> Sent: Saturday, June 29, 2002 10:25 PM Subject: Formating numbers using commas? > > Hi, > I'm trying to create a small calculator for Win32, but I can't > format numbers correctly. Is there a way to format an number / atom > using printf() or sprintf() ? I can use this code to write the number in > decimals, but I don't see any commas: sprintf("%d", 1234567). I'd like > to see: 1,234,567 I tried changing the formating numbers to %0.2d, but > that didn't work. I tried looking through the Euphoria documentation, > Win32 Demos and other programs, but I can't find any answers. > > > Any ideas or tips would be appreciated. > > Thanks! > > > >
6. Re: Formating numbers using commas?
- Posted by Dan Moyer <DANIELMOYER at prodigy.net> Jun 30, 2002
- 412 views
Juergen, Thanks, I wrote it to show the results in an attempt to make a sorted by size directory size lister (which ultimately worked fine until I tried to use it as intended, from drive letter level instead of starting from a directory, which I was never able to debug). I like "generalizing" functions so they can do more things, thanks! Dan ----- Original Message ----- From: "Juergen Luethje" <jluethje at gmx.de> To: "EUforum" <EUforum at topica.com> Sent: Sunday, June 30, 2002 10:57 AM Subject: Re: Formating numbers using commas? > > Hi Dan, > > you wrote: > > [function PrettyNumbers(object aNumber)] > > Very nice function! > > I changed it somewhat, so that the principle can be applied more > general. > > Best regards, > Juergen > > ------------------------------------------------------------------->8--- > > function block_fmt (sequence s, integer delim_char, integer block_len) > sequence ret > integer counter > > ret = {} > counter = 0 > for p = length(s) to 2 by -1 do > ret = prepend(ret, s[p]) > counter += 1 > if counter = block_len then > ret = prepend(ret, delim_char) > counter = 0 > end if > end for > > return prepend(ret, s[1]) > end function > > ------------------------------ > > include misc.e > include machine.e > > > function PrettyNumbers (integer i) > return block_fmt(sprint(i), ',', 3) > end function > > > function nibbles (integer i) > sequence ret > integer p > > ret = reverse(int_to_bits(i, 31)) + '0' > p = find('1', ret) > if p = 0 then return "0" end if > > return block_fmt(ret[p..length(ret)], ' ', 4) > end function > > ------------------------------ > > puts(1, PrettyNumbers(1234) & '\n') > puts(1, nibbles(20)) > > > >
7. Re: Formating numbers using commas?
- Posted by George Walters <gwalters at sc.rr.com> Jul 01, 2002
- 407 views
should it be 'sprintf' not 'sprint' ?? george ----- Original Message ----- From: "Don Phillips" <Graebel at hotmail.com> To: "EUforum" <EUforum at topica.com> Subject: RE: Formating numbers using commas? > > > Any ideas or tips would be appreciated. > > > > Thanks! > > Ahh I just love these little gems. Really gives the brain a good decent > workout and hones the problem solving skill set. Well (of course) I > just had to poke around with it and this is what I came up with... > > Did not do any comparisons (speed or otherwise) to those algos already > posted. > > -=-=-=-=-=-=-=-=- > > function AddCommas( atom above ) > atom below > sequence answer > > answer = {} > while above do > below = remainder( above, 1000 ) > above = floor( above / 1000 ) > if above then > answer = sprintf(",%03d",{below}) & answer > end if > end while > return( sprint(below) & answer ) > end function > > -=-=-=-=-=-=-=-=- > > Enjoy! > Don > > > >
8. Re: Formating numbers using commas?
- Posted by George Walters <gwalters at sc.rr.com> Jul 01, 2002
- 405 views
a couple of corrections function AddCommas( atom above ) atom below sequence answer answer = {} while above do below = remainder( above, 1000 ) above = floor( above / 1000 ) if above then answer = sprintf(",%03d",{below}) & answer end if end while return( sprintf("%d",below) & answer ) --<<<<<<<<<<<<<< end function (only works for whole numbers.0 george ----- Original Message ----- From: "Don Phillips" <Graebel at hotmail.com> To: "EUforum" <EUforum at topica.com> Sent: Monday, July 01, 2002 6:38 PM Subject: RE: Formating numbers using commas? > > > Any ideas or tips would be appreciated. > > > > Thanks! > > Ahh I just love these little gems. Really gives the brain a good decent > workout and hones the problem solving skill set. Well (of course) I > just had to poke around with it and this is what I came up with... > > Did not do any comparisons (speed or otherwise) to those algos already > posted. > > -=-=-=-=-=-=-=-=- > > function AddCommas( atom above ) > atom below > sequence answer > > answer = {} > while above do > below = remainder( above, 1000 ) > above = floor( above / 1000 ) > if above then > answer = sprintf(",%03d",{below}) & answer > end if > end while > return( sprint(below) & answer ) > end function > > -=-=-=-=-=-=-=-=- > > Enjoy! > Don > > > >
9. Re: Formating numbers using commas?
- Posted by Dan Moyer <DANIELMOYER at prodigy.net> Jul 05, 2002
- 403 views
Kat, Oh, yes, it did, but I misread it: I saw where you said "doesn't insert commas in it" & didn't understand that you were probably referring to the part to right of the decimal point, & didn't even test it. Sorry! I think maybe Sim did see it, though. Dan Moyer ----- Original Message ----- From: "Kat" <gertie at PELL.NET> To: "EUforum" <EUforum at topica.com> Subject: RE: Formating numbers using commas? > > Did my commatize() not get to the list? > > Kat > > > >
10. Re: Formating numbers using commas?
- Posted by Kat <gertie at PELL.NET> Jul 05, 2002
- 399 views
On 5 Jul 2002, at 13:49, Dan Moyer wrote: > > Kat, > > Oh, yes, it did, but I misread it: I saw where you said "doesn't insert > commas in it" & didn't understand that you were probably referring to the > part to right of the decimal point, & didn't even test it. Sorry! I think > maybe Sim did see it, though. I was referring to the fractional part of the number, i have never seen commas to the right of the decimal point. Kat > Dan Moyer > > ----- Original Message ----- > From: "Kat" <gertie at PELL.NET> > To: "EUforum" <EUforum at topica.com> > Sent: Wednesday, July 03, 2002 8:25 PM > Subject: RE: Formating numbers using commas? > > > > Did my commatize() not get to the list? > > > > Kat > > > > > > >