Page 1 of 1

format, cfloat, math operations

Posted: Friday 12th August 2022 6:25pm
by alfonsoC3
hi, sorry for my bad English

i have this code:

tblProductos[renglon - 1, 5].Text = "$" & cboPrecio.Text

tableview1[row,col].text = float_variable

but i need that float_variable shows with format, so i use this:

tableview1[row,col].text = format(float_variable,"$#,###,###.00")

alternative aslo i use this:

tableview1[row,col].text = format(float_variable,gb.currency)

and everyting is fine

but when i try to make math operation whith this data i need to transform to float, and here is the problem, because cfloat() don't works when i try to do this:

float_variable2 = cfloat(tableview1[row,col].text ) * 2

i gess is because tableview1[row,col].text have the date with the char "$", and that make that cfloat get an error...

so i first delete the char "$":

cfloat(mid(tableview1[row,col].text,2,len(tableview1[row,col].text))) but i still get an error...

after multiple try, i relaize that when i use format(), the instruction dont put the separator "," just put a blank space where the "," must to be... and i think is why cflots get error...

so

how i can make math operations from variables with format???

Re: format, cfloat, math operations

Posted: Saturday 13th August 2022 12:20am
by Cedron
Your English is plenty fine.

I was surprised "val" didn't work. So, when you can't find a function, roll your own. If you need to make it really fast (doubtful) you can put it in a shared library, but that is a different topic. In pure sweet code, this should work for you:

Maybe somebody still has a built in answer.

Ced
' Gambas module file

'=============================================================================
Public Sub Main()

  Print "Hello world"
  
  Dim theTestFloat As Float = 1234567.89
  Dim theTestString As String = Format(theTestFloat, gb.Currency)
  
  Print theTestString, YuckyToFloat(theTestString, "."), theTestFloat
  
End  
'=============================================================================
Function YuckyToFloat(ArgInput As String, ArgDecimalMark As String) As Float

    Dim theCollectingDecimalsFlag As Boolean = False
    Dim theDecimalFactor As Float = 1.0

    Dim p, c, d, dm As Integer
    
    dm = Asc(ArgDecimalMark)

    Dim theResult As Float = 0.0
    
    For p = 1 To Len(ArgInput)
      c = Asc(Mid(ArgInput, p, 1))   
      d = c - 48
      If d >= 0 And d <= 9 Then
         theResult = 10.0 * theResult + d
         If theCollectingDecimalsFlag Then
            theDecimalFactor *= 0.1
         Endif
      Else
         If c = dm Then theCollectingDecimalsFlag = True
      Endif
    Next
  
    Return theResult * theDecimalFactor

End
'=============================================================================
Note too, that you can cut and paste your code and place code markers (highlight, then use the 'gb' button on the toolbar) around it in the posting to make it more readable and accessable.

This is the output I got, then I used the 'code' tags.

Code: Select all

Hello world
$1,234,567.89   1234567.89      1234567.89
Edit:

After a little thinking about it, this will probably work much faster internally, but you will probably have a tough time telling unless you are calling it intensely. Just to show a few more neat Gambas' language features.
'=============================================================================
Function YuckyToFloat2(ArgInput As String, ArgDecimalMark As String) As Float

    Dim theCollectingDecimalsFlag As Boolean = False
    Dim theDecimalFactor As Float = 1.0
    
    Dim theAscCodes As Byte[] = Byte[].FromString(ArgInput)
    Dim dm As Byte = Asc(ArgDecimalMark)

    Dim a, c, d As Integer

    Dim theResult As Float = 0.0
    
    For a = 0 To theAscCodes.Max
      c = theAscCodes[a]
      d = c - 48
      If d >= 0 And d <= 9 Then
         theResult = 10.0 * theResult + d
         If theCollectingDecimalsFlag Then
            theDecimalFactor *= 0.1
         Endif
      Else
         If c = dm Then theCollectingDecimalsFlag = True
      Endif
    Next
  
    Return theResult * theDecimalFactor

End
'=============================================================================

Re: format, cfloat, math operations

Posted: Saturday 13th August 2022 2:25pm
by grayghost4
Why make it so difficult ? .... Or have I missed something ???
  Print "Hello world"
   
  Dim theTestFloat As Float = 1234567.89
    Dim theTestString As String = Format(theTestFloat, gb.Currency)
    Print "theTestString", theTestString
    
    Print Right(Replace(theTestString, ",", ""), -1)
    Print Val(Right(Replace(theTestString, ",", ""), -1)) *2
    
Returns:
Hello world
theTestString $1,234,567.89
1234567.89
2469135.78

The other caution I would suggest is that you NOT use a float variable to store and handle currency ... it will lead to trouble, I usually use a string or integer to store currency.

Re: format, cfloat, math operations

Posted: Saturday 13th August 2022 4:36pm
by cogier
The other caution I would suggest is that you NOT use a float variable to store and handle currency ... it will lead to trouble, I usually use a string or integer to store currency.
I have to agree with this. I always store currency as an integer, much safer.

Re: format, cfloat, math operations

Posted: Saturday 13th August 2022 7:20pm
by grayghost4
how i can make math operations from variables with format???
the simple answer is :

  integer_variable2 = (Val(Right(Replace(tableview1[row,col].text, ",", ""), -1)) * 100) * 2
or :
  integer_variable2 = (CFloat(Right(Replace(tableview1[row,col].text, " ", ""), -1)) * 100) * 2

I think this will work for you ;)