Adding Textfields that have $ and commas in them

I have some textfields that are part of a control set that I want to add up the data in them.
I have dragged data into them so now they have $ and commas in them.
XOJO does not inherently add them because it gets confused by the $ and , (comma)

Is there a good way to get around this to add them?
Thanks

See currency.tostring .fromstring
Maybe subclass this textbox.

1 Like

I should have been clearer. They are dragged as text with the “$” and “,”'s.

Is there a way to get them to a form to add them simply?
Converting them to double won’t get rid of the “$” and “,”'s to let them add.

Txt = txt.replace("$", "").replaceall(",", "")

From a Norman’s mind sprang forth:

suppose you have 5 text fields - 4 with data and 1 to hold the total
this code will take those text fields and add them up

Var locale As Locale = Locale.Current

Dim c1,c2, c3, c4, c5 As Currency

c1 = Currency.FromString(TextField1.Text,locale)
c2 = Currency.FromString(TextField2.Text,locale)
c3 = Currency.FromString(TextField3.Text,locale)
c4 = Currency.FromString(TextField4.Text,locale)

c5 = c1 + c2 + c3 + c4

TextField5.Text = c5.ToString(locale)

Note - the LOCALE is NOT optional as Xojo’s docs state
see http://feedback.xojo.com/case/63199

The Locale is optional if the string has only numbers and decimal point (“1123.45”), the locale is required if the number has local formatting (“$1,123.45”,“1.123,45€”).

1 Like

It’ not just “required”
A Xojo app crashes without one :slight_smile:
Thats really not helpful
https://xojo.com/issue/63199

Sub Open()
  Var locale As Locale = Locale.Current
  
  Dim c1,c2, c3, c4, c5 As Currency
  
  Try
    c1 = Currency.FromString("123.45")
    c2 = Currency.FromString("$123.45")
    c3 = Currency.FromString("$123.45",locale)
  Catch e As runtimeexception
    Break
    
  End Try
End Sub
1 Like

Markus,
yes, that’s another problem, Xojo should not crash if the locale is required and we don’t provide one.

This works:

Var c As Currency
c = Currency.FromString("123.45")

This works:

Var c As Currency
c = Currency.FromString("$123.45", locale.current)

This gives me an exception:

Var c As Currency
c = Currency.FromString("€123,45", locale.current)

This crashes Xojo:

Var c As Currency
c = Currency.FromString("$123,45")

and it shows this message:
image

Edit: it looks like the error is not a RunTimeException but a Xojo.Core.BadDataException, you can catch it with:

Var c As Currency
Try
  c = Currency.FromString("$123.45")
Catch e As Xojo.Core.BadDataException
  Break
End Try

Thanks for all the help - many different approaches to the same issue.

I stumbled across this and it worked. Yes you do have to use “locale”.

Var cellValue As String
Var locale As New Locale(“en-US”)
Var value As Currency

for ii = 0 to 6
if SecMoAmt(ii).text <> “” then
cellvalue = SecMoAmt(ii).text
value = Currency.FromString(cellValue,locale)
else
value = 0
end if
gpr = value + gpr
DepCareTotal.text = str(gpr)
next