Alaska Software Inc. - Comma() Function?
Username: Password:
AuthorTopic: Comma() Function?
Al WimberlyComma() Function?
on Fri, 09 Apr 2010 19:26:26 -0400
Anyone have the source in Xbase++ for a comma() function for string numerics 
they are willing to share.  I've been fussing with trying to build one all 
afternoon and can't get it right.

Thansk
Andreas Gehrs-Pahl
Re: Comma() Function?
on Fri, 09 Apr 2010 21:03:57 -0400
Al,

>Anyone have the source in Xbase++ for a comma() function for string numerics 
>they are willing to share.  I've been fussing with trying to build one all 
>afternoon and can't get it right.

I use this:

Function CommaFormat(nNumber, nMaxDec)
LOCAL cNumrIn  := alltrim(str(abs(iif(nMaxDec == NIL, nNumber, Round(nNumber, nMaxDec)))))
LOCAL nNumberLen := len(cNumberIn)
LOCAL nDecimals  := RAt('.', cNumberIn)
LOCAL cDecimals  := iif(nDecimals == 0, '', substr(cNumberIn, nDecimals))
LOCAL cSign      := iif(nNumber < 0, '-', '')
LOCAL cNumberOut := iif(nDecimals == 0, cNumberIn, left(cNumberIn, nDecimals - 1))
LOCAL nPos       := len(cNumberOut) - 2
   while nPos > 1
      cNumberOut := Stuff(cNumberOut, nPos, 0, ',') ; nPos -= 3
   enddo
return (cSign + cNumberOut + cDecimals)

A couple of those lines will wrap, though.

-- Andreas

---                                                                      ---
  Andreas Gehrs-Pahl              E-Mail: GPahl@CharterMI.net
  415 Gute Street                     or: Andreas@DDPSoftware.com
  Owosso, MI 48867-4410               or: Andreas_Gehrs-Pahl@CrimeCog.com
  Tel: (989) 723-9927           Web Site: http://www.Aerospace-History.net
---                                                                      ---
Andreas Gehrs-Pahl
Re: Comma() Function?
on Fri, 09 Apr 2010 21:24:08 -0400
Al,

I must have accidentally deleted two characters in the second line when 
copying the code. The correct function is like this:

Function CommaFormat(nNumber, nMaxDec)
LOCAL cNumberIn  := alltrim(str(abs(iif(nMaxDec == NIL, nNumber, Round(nNumber, nMaxDec)))))
LOCAL nNumberLen := len(cNumberIn)
LOCAL nDecimals  := RAt('.', cNumberIn)
LOCAL cDecimals  := iif(nDecimals == 0, '', substr(cNumberIn, nDecimals))
LOCAL cSign      := iif(nNumber < 0, '-', '')
LOCAL cNumberOut := iif(nDecimals == 0, cNumberIn, left(cNumberIn, nDecimals - 1))
LOCAL nPos       := len(cNumberOut) - 2
   while nPos > 1
      cNumberOut := Stuff(cNumberOut, nPos, 0, ',') ; nPos -= 3
   enddo
return (cSign + cNumberOut + cDecimals)

-- Andreas

---                                                                      ---
  Andreas Gehrs-Pahl              E-Mail: GPahl@CharterMI.net
  415 Gute Street                     or: Andreas@DDPSoftware.com
  Owosso, MI 48867-4410               or: Andreas_Gehrs-Pahl@CrimeCog.com
  Tel: (989) 723-9927           Web Site: http://www.Aerospace-History.net
---                                                                      ---
mirda Re: Comma() Function?
on Sat, 10 Apr 2010 13:19:58 +0200
On Fri, 09 Apr 2010 19:26:26 -0400, Al Wimberly wrote:

> Anyone have the source in Xbase++ for a comma() function for string numerics 
> they are willing to share.  I've been fussing with trying to build one all 
> afternoon and can't get it right.
> 
> Thansk

Hi Al!

Why don't you use function Transform(nNumber, "999,999,999,999.99")?

mirda
Andreas Gehrs-Pahl
Re: Comma() Function?
on Sat, 10 Apr 2010 12:27:07 -0400
Mirda,

>Why don't you use function Transform(nNumber, "999,999,999,999.99")?

You could use Transform(), but to be equivalent, you would need to also
do an alltrim() or LTrim(), to get rid of the leading spaces. Also, you
would need to adjust the string to use the desired number of decimals.

The main reason I (still) use a function, is that Xbase++ had a lot 
of issues over the years with Transform() and various Picture Clauses, 
which forced me to write my own functions instead. It looks like in 
this particular case that it is fine to use Transform() -- at least 
with the latest Xbase++ version. 

-- Andreas

---                                                                      ---
  Andreas Gehrs-Pahl              E-Mail: GPahl@CharterMI.net
  415 Gute Street                     or: Andreas@DDPSoftware.com
  Owosso, MI 48867-4410               or: Andreas_Gehrs-Pahl@CrimeCog.com
  Tel: (989) 723-9927           Web Site: http://www.Aerospace-History.net
---                                                                      ---