B4J Question [SOLVED] How To Get Precise B4XTable Column Numbers

cklester

Well-Known Member
Licensed User
Check out the attached simple B4XTable app.

The numbers in each column come from the same source, but they are often displayed as two wildly different numbers!

Can anybody explain why, and how I can make sure that any numbers I display with COLUMN_TYPE_NUMBERS are as precise as can be?

I'm not sure if it's the MyNumberString.As(Float) that is problematic, or B4XTable munging the numbers.

Who knows?


This has been resolved with the use of a column formatter (B4XFormatter). Check out the attached app to see how it works.

There's also a random number generator that gets you anything from "0.0" to "999999999.999999999." It's handy for testing purposes. (Might could be made faster, but it's already fast enough.)
 

Attachments

  • b4xtable_column_formats_test_final.zip
    9.7 KB · Views: 142
Last edited:

LucaMs

Expert
Licensed User
Longtime User
First:
Your function:
Public Sub randomStringOfNumbers(len As Int) As String
    Dim numbers As String = "0123456789"
    Dim theNum As StringBuilder
    theNum.Initialize
    Dim numChars As Int = Rnd(1, len)
    Dim i As Int
    For i=0 To numChars-1
        theNum.Append(numbers.CharAt(Rnd(0,numbers.Length-1)))
    Next
    Return theNum
End Sub
More simple:
Public Sub randomStringOfNumbers(len As Int) As String
    Return "0123456789".SubString2(0, len)
End Sub
 
Upvote 0

cklester

Well-Known Member
Licensed User
I don't understand what you mean; the two functions do the exact same job, they return the same result.

Huh? They don't produce the same result at all.

Your function could never return a number like "4859.33392" whereas mine could.

Your function returns random-length numbers that ALWAYS start with "0".

Mine produces a random-length string numbers of random numbers.

If you want to create random numbers, this has nothing to do with the simplification I suggested.

The whole point of the function I posted is to create a random-length string of random numbers.
 
Upvote 0

cklester

Well-Known Member
Licensed User
Your function produces:

1638251485950.png


My function produces:

1638251511368.png


Not the same at all...
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Huh? They don't produce the same result at all.

Your function could never return a number like "4859.33392" whereas mine could.

Your function returns random-length numbers that ALWAYS start with "0".

Mine produces a random-length string numbers of random numbers.



The whole point of the function I posted is to create a random-length string of random numbers.
Yes, I realized they were random; I've seen them consecutive! (distracted).

However that routine is slow, that way.
 
Upvote 0

cklester

Well-Known Member
Licensed User
The only way to get exactly what you want is to use B4XFormatter; unless you also set the second column as text and use NumberFormat2.

I'm getting the number as a string, so NumberFormat2 doesn't help. I'd like to have them as numbers so they sort properly, so I'll look into the B4XFormatter.

Thank you!
 
Upvote 0

cklester

Well-Known Member
Licensed User
I meant in your randomStringOfNumbers, which returns a string.

Yes, but there's never an actual number (float, double, etc.)... The number sent to the function determines the maximum length of the string of random digits.

But you cannot insert a string-number in a numeric column and be able to obtain the desired formatting.
The number to insert must be... a number (better a Double) and then format it using B4XFormatter.

I take the string generated by the function and turn it into a float to insert into the B4XTable, so there is an Array of Object(string, float) being put into the B4XTable. i.e., there is no string being put into a number column. It's all done properly.

The problem was, it looks like the format of the column is limited to three decimal places or 8-9 digits in length.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Yes, but there's never an actual number (float, double, etc.)... The number sent to the function determines the maximum length of the string of random digits.



I take the string generated by the function and turn it into a float to insert into the B4XTable, so there is an Array of Object(string, float) being put into the B4XTable. i.e., there is no string being put into a number column. It's all done properly.

The problem was, it looks like the format of the column is limited to three decimal places or 8-9 digits in length.
They are certainly default values; 3 decimals for double values, I suppose.

However, you also want to get a fixed length for the whole part, don't you? Also for this you will need B4XFormatter.
 
Upvote 0

cklester

Well-Known Member
Licensed User
However, you also want to get a fixed length for the whole part, don't you? Also for this you will need B4XFormatter.

Eventually, I'd like to line up the decimal in the column, but I'm taking baby steps... 😁

The column has to be numbers, because otherwise it won't sort properly. :( Unless I can specify somewhere for strings to sort like numbers!
 
Upvote 0
Top