Date formatting...

Questions regarding syntax

Re: Date formatting...

Postby cpcarranza » Fri Sep 03, 2010 3:26 pm

Thanks a lot ! :D

That was very usefull, Thank you both !

I hope Bernd fixs it soon, so we can keep trying both the beta and stable releases....

P.S. I did a short func without the "," since We only use "," in this country 8-)

Thanks and Good Luck !
cpcarranza
 
Posts: 10
Joined: Mon Jan 11, 2010 11:32 pm

Re: Date formatting...

Postby pappawinni » Fri Sep 03, 2010 3:54 pm

Hi cp,

This means you just wanted a comma as decimal character
and do not need the thousands separator, right ?

/Winni
Pappa makes everything what otherwise none likes :)
pappawinni
 
Posts: 192
Joined: Tue Jan 19, 2010 11:27 pm
Location: Germany

Re: Date formatting...

Postby Slowdown » Fri Sep 03, 2010 6:57 pm

@pappawinni
P.S. I have a similar problem with FORMAT using " , " for numbers.... I want "1,000" but always get "1000"

That's why i started with my (not correct working) function.
Do you know what's the problem is at this moment, i'm thinking to start writing it again and this time
a correct working version :roll:

why is this one pulling on me :twisted:
Regards
Slowdown for now i'm back
Slowdown
 
Posts: 347
Joined: Sat May 02, 2009 6:48 pm
Location: Netherlands

Re: Date formatting...

Postby pappawinni » Fri Sep 03, 2010 7:37 pm

Hi Slowdown,

Worked on yours a bit..
.. but am not really satisfied with it..

Code: Select all
Private function FormatNum(fNum as Double, fStr as String, lfttr as Boolean = True, rttr as boolean = True) as String
  Dim thdSep as String = ","
  Dim DecSep as String = "."
  Dim LeftFormat as Integer
  Dim RightFormat as Integer
  Dim Lus as Integer
  Dim CharCount as Integer = 0
  Dim LusStr As String
  Dim strSign as string=""
  Dim dblNum as double
  Dim intRemain as integer
  Dim strRemain as String
  Dim intPreDec as integer
  Dim strPreDec as String
  Dim iSignific as integer
  Dim iSep as integer
     
  dblNum = fNum
 
  If fNum<0 then
    dblNum = abs(dblNum)
    strSign = "-"
  End if 
 
  LeftFormat = (instr(1, fStr, ".")) - 1
  RightFormat = len(fStr) - LeftFormat - 1
  If Instr(1, fStr, ",") >= 1 then 
    DecSep = ","
    thdSep = "."
    LeftFormat = (instr(1, fStr, ",")) - 1
    RightFormat = len(fStr) - LeftFormat - 1
  End If

  intPreDec = int(dblNum)
  strPreDec = strSign & trim(format(intPreDec,"###############."))


  if dblNum - intPreDec > 0 Then
    intRemain = int((dblNum - intPreDec) * 1e10)
    strRemain = right("000000000000000" & trim(format(intRemain,"###############.")),10)
  else
    strRemain = ""
  End If
  iSignific = len( val( StrReverse(strRemain) & "1" ) ) - 1
  strRemain = Left( strRemain, iSignific )
 
? dblNum, intPredec, intRemain

  iSep = int(log(dblNum) / log(10.0)) \ 3
 
  CharCount = 0
  For Lus = Len(strPreDec) to 1 step -1
    CharCount = CharCount + 1
    LusStr = Mid(strPreDec, Lus, 1) & LusStr
    If CharCount = 3 and iSep > 0  Then
      LusStr = thdSep & LusStr
      CharCount = 0
      iSep = iSep - 1
    End If
  Next
 
  strPreDec = LusStr
 
  if lfttr = True and LeftFormat > len(strPreDec) Then
    strPreDec = Space(LeftFormat - Len(strPreDec)) & strPreDec
  End If
   
  If rttr = True Then
    strRemain = Left(strRemain + Fill("0", "0", RightFormat), RightFormat)
  End If
  Return strPreDec & DecSep & strRemain
End Function
Pappa makes everything what otherwise none likes :)
pappawinni
 
Posts: 192
Joined: Tue Jan 19, 2010 11:27 pm
Location: Germany

Re: Date formatting...

Postby Slowdown » Sat Sep 04, 2010 5:40 am

Hi pappawinni,
Can you help me with the following ?
found something i can't explain.
Code: Select all
Private Sub Form_OnOpen()
  Dim RInt as integer
  dim RDoub as double
  dim RSingle as single
  dim RCur as currency
  dim RDec as Decimal
  dim RStr as String
  dim RVar as Variant
 
  RInt=22/7
  RDoub=22/7
  RSingle=22/7
  RCur=22/7
  RDec=22/7
  RStr=22/7
  RVar=22/7
 
  log "Integer 22/7  = " & RInt
  log "Double 22/7   = " & RDoub
  log "Single 22/7   = " & RSingle
  log "Currency 22/7 = " & RCur
  log "Decimal 22/7  = " & RDec
  log "String 22/7   = " & RStr
  log "Variant 22/7  = " & RVar
End Sub

The output of RCur and RDec are in my opinion strange.
Regards
Slowdown for now i'm back
Slowdown
 
Posts: 347
Joined: Sat May 02, 2009 6:48 pm
Location: Netherlands

Re: Date formatting...

Postby cpcarranza » Sat Sep 04, 2010 5:10 pm

@ pappawinni

Hi cp,

This means you just wanted a comma as decimal character
and do not need the thousands separator, right ?


Hi

No, I'm from Mexico and use the comma as the thousands separator: 1,000,000.00
I just mean that I will change the function's parameters and will avoid using: strThousandSep as String = "", actualy I will put the function in a module for re-using on other programas and will be call: formatNum :D

Thanks !
cpcarranza
 
Posts: 10
Joined: Mon Jan 11, 2010 11:32 pm

Re: Date formatting...

Postby pappawinni » Sun Sep 05, 2010 6:10 pm

Slowdown wrote:...The output of RCur and RDec are in my opinion strange.

autsch ... really strange.

and if you want PI.. then dont use 22/7 but use a double PI and
PI = atn(1.0) * 4.0
for outout do not just use "Print Pi" or "log PI" but use e.g. "log format( PI , "X.XXXXXXXXXXXXXX")"
or my XStr-function (Board "User functions") :lol:
Pappa makes everything what otherwise none likes :)
pappawinni
 
Posts: 192
Joined: Tue Jan 19, 2010 11:27 pm
Location: Germany

Re: Date formatting...

Postby Slowdown » Sun Sep 05, 2010 6:31 pm

Was thinking about my own Format function and did some testing that's why the 22/7 ;)
for outout do not just use "Print Pi" or "log PI" but use e.g. "log format( PI , "X.XXXXXXXXXXXXXX")"
or my XStr-function (Board "User functions")

Yeah right that's the point with your function or the KBasic Format function it will be to easy :D
Regards
Slowdown for now i'm back
Slowdown
 
Posts: 347
Joined: Sat May 02, 2009 6:48 pm
Location: Netherlands

Re: Date formatting...

Postby pappawinni » Sun Sep 05, 2010 8:34 pm

Hi Slowdown,

Think the strategie (algorithm) then should be.. let me think...

- take absolute value but keep sign in mind
- Analyse the format and find how to transform the number given.
- Transform (round, cut or so) the number as far as necessary
- if necessary (could also just be .1234) take the left part of the number, convert to a string and insert thousands-seperators as far as necessary
- if necessary put sign
- if necessary (could also just be 123 ) take the right part of the number, convert to a string)
- if required, replace "." by "," and vice versa, probably take some totally different placeholders first?

For the right part (Remainder) - if you do not want to use format function - you may need a special approach.
perhaps something like this:
Code: Select all
sub main ()

dim dblPi as double
dim iC as integer
dim i as integer
dim iSignific as integer
dim iRemainder as integer
dim strRemainder as string
dim dblRemainder as double

dblPi = atn(1)*4

dblRemainder = dblPi - dblPi \ 1
'required number of decimals
iRemainder = 6

'round
if iRemainder <16 then dblRemainder = dblRemainder + 0.5 * (10.0)^(-iRemainder)
'create string
for i = 1 to iRemainder
  dblremainder = dblremainder * 10
  ic = iif(i<16 , dblremainder \ 1 , 0 )
  dblremainder = dblremainder - ic
  strRemainder = strRemainder & ic
next
'find significant decimals
iSignific = iRemainder
for i = iRemainder to 1 Step -1
  if mid(strRemainder,i,1)="0" then
    iSignific = iSignific - 1
  else
    i=1
  end if
next
'output
? strRemainder
? iSignific
end sub



main()
Pappa makes everything what otherwise none likes :)
pappawinni
 
Posts: 192
Joined: Tue Jan 19, 2010 11:27 pm
Location: Germany

Previous

Return to Coding Questions

cron