format, cfloat, math operations

Post

Posted
Rating:
#1 (In Topic #887)
Trainee
 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???

Image

(Click to enlarge)

Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
Cedron is in the usergroup ‘Regular’
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

Code (gambas)

  1. ' Gambas module file
  2.  
  3. '=============================================================================
  4. Public Sub Main()
  5.  
  6.   Print "Hello world"
  7.  
  8.   Dim theTestFloat As Float = 1234567.89
  9.   Dim theTestString As String = Format(theTestFloat, gb.Currency)
  10.  
  11.   Print theTestString, YuckyToFloat(theTestString, "."), theTestFloat
  12.  
  13. End  
  14. '=============================================================================
  15. Function YuckyToFloat(ArgInput As String, ArgDecimalMark As String) As Float
  16.  
  17.     Dim theCollectingDecimalsFlag As Boolean = False
  18.     Dim theDecimalFactor As Float = 1.0
  19.  
  20.     Dim p, c, d, dm As Integer
  21.    
  22.     dm = Asc(ArgDecimalMark)
  23.  
  24.     Dim theResult As Float = 0.0
  25.    
  26.     For p = 1 To Len(ArgInput)
  27.       c = Asc(Mid(ArgInput, p, 1))  
  28.       d = c - 48
  29.       If d >= 0 And d <= 9 Then
  30.          theResult = 10.0 * theResult + d
  31.          If theCollectingDecimalsFlag Then
  32.             theDecimalFactor *= 0.1
  33.          Endif
  34.       Else
  35.          If c = dm Then theCollectingDecimalsFlag = True
  36.       Endif
  37.     Next
  38.  
  39.     Return theResult * theDecimalFactor
  40.  
  41. '=============================================================================
  42.  

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

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.

Code (gambas)

  1. '=============================================================================
  2. Function YuckyToFloat2(ArgInput As String, ArgDecimalMark As String) As Float
  3.  
  4.     Dim theCollectingDecimalsFlag As Boolean = False
  5.     Dim theDecimalFactor As Float = 1.0
  6.    
  7.     Dim theAscCodes As Byte[] = Byte[].FromString(ArgInput)
  8.     Dim dm As Byte = Asc(ArgDecimalMark)
  9.  
  10.     Dim a, c, d As Integer
  11.  
  12.     Dim theResult As Float = 0.0
  13.    
  14.     For a = 0 To theAscCodes.Max
  15.       c = theAscCodes[a]
  16.       d = c - 48
  17.       If d >= 0 And d <= 9 Then
  18.          theResult = 10.0 * theResult + d
  19.          If theCollectingDecimalsFlag Then
  20.             theDecimalFactor *= 0.1
  21.          Endif
  22.       Else
  23.          If c = dm Then theCollectingDecimalsFlag = True
  24.       Endif
  25.     Next
  26.  
  27.     Return theResult * theDecimalFactor
  28.  
  29. '=============================================================================
  30.  

.... and carry a big stick!
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
Why make it so difficult ? …. Or have I missed something ???

Code (gambas)

  1.   Print "Hello world"
  2.    
  3.   Dim theTestFloat As Float = 1234567.89
  4.     Dim theTestString As String = Format(theTestFloat, gb.Currency)
  5.     Print "theTestString", theTestString
  6.    
  7.     Print Right(Replace(theTestString, ",", ""), -1)
  8.     Print Val(Right(Replace(theTestString, ",", ""), -1)) *2
  9.    

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.
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
 how i can make math operations from variables with format???

the simple answer is :


Code (gambas)

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

Code (gambas)

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


I think this will work for you  ;)
Online now: No Back to the top
1 guest and 0 members have just viewed this.