cklester Well-Known Member Licensed User Nov 30, 2021 #1 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 b4xtable_column_formats_test_final.zip 9.7 KB · Views: 577 Last edited: Nov 30, 2021
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.)
LucaMs Expert Licensed User Longtime User Nov 30, 2021 #2 First: Code: Copy 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 Code: Copy Public Sub randomStringOfNumbers(len As Int) As String Return "0123456789".SubString2(0, len) End Sub Upvote 0
First: Code: Copy 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 Code: Copy Public Sub randomStringOfNumbers(len As Int) As String Return "0123456789".SubString2(0, len) End Sub
cklester Well-Known Member Licensed User Nov 30, 2021 #3 LucaMs said: Code: Copy Public Sub randomStringOfNumbers(len As Int) As String Return "0123456789".SubString2(0, len) End Sub Click to expand... Sure... For this test, that would suffice, but for a random string of numbers, it would not suffice. ? Upvote 0
LucaMs said: Code: Copy Public Sub randomStringOfNumbers(len As Int) As String Return "0123456789".SubString2(0, len) End Sub Click to expand... Sure... For this test, that would suffice, but for a random string of numbers, it would not suffice. ?
LucaMs Expert Licensed User Longtime User Nov 30, 2021 #4 cklester said: Sure... For this test, that would suffice, but for a random string of numbers, it would not suffice. Click to expand... I don't understand what you mean; the two functions do the exact same job, they return the same result. If you want to create random numbers, this has nothing to do with the simplification I suggested. Upvote 0
cklester said: Sure... For this test, that would suffice, but for a random string of numbers, it would not suffice. Click to expand... I don't understand what you mean; the two functions do the exact same job, they return the same result. If you want to create random numbers, this has nothing to do with the simplification I suggested.
LucaMs Expert Licensed User Longtime User Nov 30, 2021 #5 You can use: B4X: Copy allTheRows.Add(Array(theValue, theValue.As(Double))) but it will only display 3 decimal places. To get exactly what you want, you should use B4XFormatter for that column. Upvote 0
You can use: B4X: Copy allTheRows.Add(Array(theValue, theValue.As(Double))) but it will only display 3 decimal places. To get exactly what you want, you should use B4XFormatter for that column.
cklester Well-Known Member Licensed User Nov 30, 2021 #6 LucaMs said: I don't understand what you mean; the two functions do the exact same job, they return the same result. Click to expand... 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. LucaMs said: If you want to create random numbers, this has nothing to do with the simplification I suggested. Click to expand... The whole point of the function I posted is to create a random-length string of random numbers. Upvote 0
LucaMs said: I don't understand what you mean; the two functions do the exact same job, they return the same result. Click to expand... 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. LucaMs said: If you want to create random numbers, this has nothing to do with the simplification I suggested. Click to expand... The whole point of the function I posted is to create a random-length string of random numbers.
cklester Well-Known Member Licensed User Nov 30, 2021 #7 Your function produces: My function produces: Not the same at all... Upvote 0
LucaMs Expert Licensed User Longtime User Nov 30, 2021 #8 cklester said: 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. Click to expand... Yes, I realized they were random; I've seen them consecutive! (distracted). However that routine is slow, that way. Upvote 0
cklester said: 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. Click to expand... Yes, I realized they were random; I've seen them consecutive! (distracted). However that routine is slow, that way.
LucaMs Expert Licensed User Longtime User Nov 30, 2021 #9 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. Upvote 0
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.
cklester Well-Known Member Licensed User Nov 30, 2021 #10 LucaMs said: You can use: B4X: Copy allTheRows.Add(Array(theValue, theValue.As(Double))) Click to expand... I thought a double was an int... (didn't help anyway) LucaMs said: but it will only display 3 decimal places. Click to expand... Ah... So is that a limitation of the basic B4XTable? LucaMs said: To get exactly what you want, you should use B4XFormatter for that column. Click to expand... Thank you! I'll look for that. Upvote 0
LucaMs said: You can use: B4X: Copy allTheRows.Add(Array(theValue, theValue.As(Double))) Click to expand... I thought a double was an int... (didn't help anyway) LucaMs said: but it will only display 3 decimal places. Click to expand... Ah... So is that a limitation of the basic B4XTable? LucaMs said: To get exactly what you want, you should use B4XFormatter for that column. Click to expand... Thank you! I'll look for that.
cklester Well-Known Member Licensed User Nov 30, 2021 #11 LucaMs said: However that routine is slow, that way. Click to expand... How to make faster? ? Upvote 0
LucaMs Expert Licensed User Longtime User Nov 30, 2021 #12 cklester said: How to make faster? ? Click to expand... By using 2 number variables and NumberFormat2 to "join" them. Upvote 0
cklester said: How to make faster? ? Click to expand... By using 2 number variables and NumberFormat2 to "join" them.
cklester Well-Known Member Licensed User Nov 30, 2021 #13 LucaMs said: 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. Click to expand... 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
LucaMs said: 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. Click to expand... 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!
LucaMs Expert Licensed User Longtime User Nov 30, 2021 #14 cklester said: I'm getting the number as a string, so NumberFormat2 doesn't help. Click to expand... I meant in your randomStringOfNumbers, which returns a string. 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. Upvote 0
cklester said: I'm getting the number as a string, so NumberFormat2 doesn't help. Click to expand... I meant in your randomStringOfNumbers, which returns a string. 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.
cklester Well-Known Member Licensed User Nov 30, 2021 #15 LucaMs said: I meant in your randomStringOfNumbers, which returns a string. Click to expand... 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. LucaMs said: 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. Click to expand... 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 said: I meant in your randomStringOfNumbers, which returns a string. Click to expand... 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. LucaMs said: 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. Click to expand... 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.
LucaMs Expert Licensed User Longtime User Nov 30, 2021 #16 cklester said: 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. Click to expand... 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 said: 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. Click to expand... 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.
LucaMs Expert Licensed User Longtime User Nov 30, 2021 #17 cklester said: How to make faster? ? Click to expand... B4X: Copy Public Sub randomStringOfNumbers(len As Int) As String Dim Left As Int = Rnd(0, Power(10, len)) Dim Right As Int = Rnd(0, Power(10, len)) Return NumberFormat2(Left, len, 0, 0, False) & "." & NumberFormat2(Right, len, 0, 0, False) End Sub Upvote 0
cklester said: How to make faster? ? Click to expand... B4X: Copy Public Sub randomStringOfNumbers(len As Int) As String Dim Left As Int = Rnd(0, Power(10, len)) Dim Right As Int = Rnd(0, Power(10, len)) Return NumberFormat2(Left, len, 0, 0, False) & "." & NumberFormat2(Right, len, 0, 0, False) End Sub
cklester Well-Known Member Licensed User Nov 30, 2021 #18 LucaMs said: However, you also want to get a fixed length for the whole part, don't you? Also for this you will need B4XFormatter. Click to expand... 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
LucaMs said: However, you also want to get a fixed length for the whole part, don't you? Also for this you will need B4XFormatter. Click to expand... 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!
LucaMs Expert Licensed User Longtime User Nov 30, 2021 #19 I don't know the final purpose. Theoretically you could set that column as text and pass it numbers formatted with NumberFormat2. It would be the simplest solution. But learning B4XFormatter is also helpful. Upvote 0
I don't know the final purpose. Theoretically you could set that column as text and pass it numbers formatted with NumberFormat2. It would be the simplest solution. But learning B4XFormatter is also helpful.
cklester Well-Known Member Licensed User Nov 30, 2021 #20 LucaMs said: Theoretically you could set that column as text and pass it numbers formatted with NumberFormat2. Click to expand... Unfortunately, I couldn't. The way it sorts string numbers is problematic. Here's the table with the text-numbers sorted: Upvote 0
LucaMs said: Theoretically you could set that column as text and pass it numbers formatted with NumberFormat2. Click to expand... Unfortunately, I couldn't. The way it sorts string numbers is problematic. Here's the table with the text-numbers sorted: