B4J Question [SOLVED] Number formatter B4XTable

Peter Lewis

Active Member
Licensed User
Longtime User
Hi

I am trying to make a B4XTable cells showing 2 decimal places regardless if they have values or not.

In the Table I have formatted the Cell as Number

and this is the code I have been trying without any success

Formatting code:
    formatter.Initialize
    Dim DefaultFormat As B4XFormatData = formatter.GetDefaultFormat
    formatter.GetDefaultFormat.MaximumFractions = 2
    formatter.GetDefaultFormat.MinimumFractions = 2
    
    tblInvDetail.DefaultDataFormatter.GetDefaultFormat

Any Suggestions would be great

Thank you
 

TILogistic

Expert
Licensed User
Longtime User
you have to round the value to 2 decimal places

see example
B4X:
Public Sub TestFormatter
    Dim out As String
   
    out = out & CRLF & SetFormmatter("12345.678", 2)
    out = out & CRLF & SetFormmatter("3.45", 2)
    out = out & CRLF & SetFormmatter("123", 2)
    out = out & CRLF & SetFormmatter("2456000.3", 2)
   
    Log(out)
End Sub

Public Sub SetFormmatter( Value As String, Decimal As Int) As String
    Dim formatter As B4XFormatter
    formatter.Initialize
    formatter.GetDefaultFormat.GroupingCharacter = ","
    formatter.GetDefaultFormat.DecimalPoint = "."
    formatter.GetDefaultFormat.MaximumFractions = Decimal
    formatter.GetDefaultFormat.MinimumFractions = Decimal
    Dim Data As Double = Round2(Value, Decimal)
    Return formatter.Format(Data)
End Sub
1639771704141.png


see:
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
So if I understand you correctly , I should not use the tblInvDetail.COLUMN_TYPE_NUMBERS in the cell. I should use the formatter and make it a String ?
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
just round the value to 2 decimal places, the one you want to display in the column
see
B4X:
Public Sub TestFormatter
    Dim out As String
    
    out = out & CRLF & SetFormmatter(12345.678, 2)
    out = out & CRLF & SetFormmatter(3.45, 2)
    out = out & CRLF & SetFormmatter(123, 2)
    out = out & CRLF & SetFormmatter(2456000.3, 2)
    out = out & CRLF & SetFormmatter(0, 2)
    out = out & CRLF & SetFormmatter(-1, 2)
    
    Log(out)
End Sub

Public Sub SetFormmatter(Value As Double, Decimal As Int) As String
    Dim DataFormatter As B4XFormatter
    DataFormatter.Initialize
    DataFormatter.GetDefaultFormat.MaximumFractions = Decimal
    DataFormatter.GetDefaultFormat.MinimumFractions = Decimal
    Return DataFormatter.Format(Round2(Value,2))
End Sub

1639780755447.png
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
post the routine where you assign the values to the b4xtable.

so they can help you

or

see:
 
Upvote 0
Top