format, cfloat, math operations

Post your Gambas programming questions here.
Post Reply
alfonsoC3
Posts: 1
Joined: Thursday 11th August 2022 1:47pm

format, cfloat, math operations

Post 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???
Attachments
Captura de pantalla de 2022-08-12 13-23-16.png
Captura de pantalla de 2022-08-12 13-23-16.png (94.56 KiB) Viewed 1093 times
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

Re: format, cfloat, math operations

Post 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
'=============================================================================
.... and carry a big stick!
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: format, cfloat, math operations

Post 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.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: format, cfloat, math operations

Post 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.
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: format, cfloat, math operations

Post 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 ;)
Post Reply