Question about local currency number format

Yafuhenk

Active Member
Licensed User
Longtime User
Hi,

I have a database with turnover imported from a csv file.
In the Netherlands we have the following format 12.345,67 EUR.
I can't find a way to add the figures. B4A sees it as text.

As far as I can see the AHLocal libary isn't useful here correct?
Also Numberformat doesn't solve my problem.

Any ideas how I can solve this?

Thanks

Henk
 

klaus

Expert
Licensed User
Longtime User
I'm afraid that you need to do it on your own.
You could have something like the following:
B4X:
Dim Amount As String
Dim Value As Double

Amount = "12.234,56 EUR"
Amount = Amount.SubString2(0, Amount.Length - 4)   ' removes EUR
Amount = Amount.Replace(".", "")                   ' removes the '.'
Amount = Amount.Replace(",", ".")                  ' replaces ',' by '.'
Value = Amount                                     ' transforms String to Double
Best regards.
 
Upvote 0

Yafuhenk

Active Member
Licensed User
Longtime User
Thanks Klaus, I'll give it a try.
One should however not run this script twice in the same database because
First time 12.345,67 gives 12345.67
Second time 12345.67 gives 1234567
It's a good way to increase turnover

Henk
 
Upvote 0

Yafuhenk

Active Member
Licensed User
Longtime User
Yes, that solved my problem.
What I do not understand however is why I can´t format the number.
Here are the steps:

Reading the csv file and creating the database:
B4X:
Sub btnImport_Click
   DoEvents
   pgbMain.Progress = 0
   pgbMain.Visible = True
   List1.Initialize
   List1 = StringUtils1.LoadCSV(File.DirAssets, "test3.csv", ";")
   SQL1.Initialize(File.DirRootExternal, "test1.db", True)
   SQL1.ExecNonQuery("DROP TABLE IF EXISTS table1")
   SQL1.ExecNonQuery("CREATE TABLE table1 (col1 TEXT, col2 TEXT, col3 TEXT, col4 TEXT, col5 TEXT, col6 TEXT, col7 TEXT, col8 TEXT, col9 TEXT, col10 TEXT, col11 TEXT, col12 TEXT, col13 REAL, col14 TEXT)")
   SQL1.BeginTransaction
   Try
      For I = 0 To List1.Size-1
         Dim ColumnContent() As String
         Dim Amount As String      
         Dim Value As Double
         ColumnContent = List1.Get(I)
         If ColumnContent(10) = "Material" OR ColumnContent(10) = "Result" OR ColumnContent(10) = "#" Then
         Else
            Amount = ColumnContent(12)
            Amount = Amount.SubString2(0, Amount.Length - 4)   ' removes EUR
            Amount = Amount.Replace(".", "")                   ' removes the '.'
            Amount = Amount.Replace(",", ".")                  ' replaces ',' by '.'
            Value = Amount
            SQL1.ExecNonQuery2("INSERT INTO table1 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)", Array As Object (ColumnContent(0), ColumnContent(1), ColumnContent(2), ColumnContent(3), ColumnContent(4), ColumnContent(5), ColumnContent(6), ColumnContent(7), ColumnContent(8), ColumnContent(9), ColumnContent(10), ColumnContent(11), Value, ColumnContent(13)))
         End If
         DoEvents
         pgbMain.Progress = I * 100 / List1.Size-1
      Next
      DoEvents
      pgbMain.Progress = 100
      SQL1.TransactionSuccessful
      pgbMain.Visible = False
      
   Catch
      Log(LastException.Message)
   End Try
   SQL1.EndTransaction
End Sub

And here a modified part of the scrollview example where I try to format the number without succes:

B4X:
Sub AddRow(Values() As String)
   If values.Length <> NumberOfColumns Then
      Log("Wrong number of values.")
      Return
   End If
   Dim lastRow As Int
   lastRow = NumberOfRows
   For i = 0 To NumberOfColumns - 1
      Dim l As Label
      l.Initialize("cell")
      l.Text = values(i)
      If i = ColumnTurnover Then
         l.Text= NumberFormat(l.Text,0,2)
         l.Gravity = Gravity.RIGHT
      Else
         l.Gravity = Gravity.LEFT
      End If
      'l.Gravity = Alignment
      l.TextSize = FontSize
      l.TextColor = FontColor
      Dim rc As RowCol
      rc.Initialize
      rc.Col = i
      rc.Row = lastRow
      l.Tag = rc
      Table.AddView(l, ColumnWidth * i, RowHeight * lastRow, ColumnWidth, RowHeight)
   Next
   Table.Height = NumberOfRows * RowHeight

ColumnTurnover is defined as follows:
B4X:
Sub SetHeader(Values() As String)
   'If header.IsInitialized Then Return 'should only be called once
   header.Initialize("")
   For i = 0 To NumberOfColumns - 1
      Dim l As Label
      l.Initialize("header")
      l.Text = values(i)
      If l.Text.IndexOf("EUR")>0 Then
         ColumnTurnover = i
         l.Gravity = Gravity.RIGHT
      Else
         l.Gravity = Gravity.LEFT
      End If
      l.TextSize = FontSize
      l.Color = HeaderColor
      l.TextColor = HeaderFontColor
      l.Tag = i
      header.AddView(l, ColumnWidth * i, 0, ColumnWidth, RowHeight)
   Next
   Activity.AddView(header, sv.Left, sv.Top - RowHeight, sv.Width, RowHeight)
End Sub

Is there anyone who can tell me why
B4X:
l.Text= NumberFormat(l.Text,0,2)
doesn´t work. It simple leaves the text unchanged.

Thanks for any feedback

Henk
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…