format, cfloat, math operations
Posted
#1
(In Topic #887)
Trainee
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???
Posted
Regular

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)
- ' Gambas module file
- '=============================================================================
- Print "Hello world"
- '=============================================================================
- d = c - 48
- theResult = 10.0 * theResult + d
- theDecimalFactor *= 0.1
- Return theResult * theDecimalFactor
- '=============================================================================
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)
- '=============================================================================
- c = theAscCodes[a]
- d = c - 48
- theResult = 10.0 * theResult + d
- theDecimalFactor *= 0.1
- Return theResult * theDecimalFactor
- '=============================================================================
.... and carry a big stick!
Posted
Enthusiast

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.
Posted
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.
Posted
Enthusiast

how i can make math operations from variables with format???
the simple answer is :
I think this will work for you
1 guest and 0 members have just viewed this.



