How to determine the length of the integer of a number converted to a string

Mahares

Expert
Licensed User
Longtime User
I start with a number, obtain its integer, convert the integer to a string and then return the length of the string:
Here is my example:X=9980.76
NumberFormat (9980.76,0,0).length yields 5. Why am I getting 5 when it should be 4?
The integer of X is 9980. Then the length of the rounded X is 4.
When I perform the same operation on X=12.689 , NumberFormat (12.689,0,0).length , the result yields a length =2 which is correct. I am puzzled with these conflicting results after spending hours trying to figure this out. Any help or a better approach to the problem is highly appreciated.
Thank you
 

timo

Active Member
Licensed User
Longtime User
B4X:
Dim x As Double
Dim y,j As Int
Dim z As String

x= 9980.76
   Log (x) 'gives 9980,76
y=x '(even though i prefer to convert with other methods than this one) 
   Log(y) 'gives 9980
   
z=NumberFormat(x,0,0)
   Log(z) 'gives 9.981
   
j=z.Length
   Log(j) 'gives 5 [correct: it'a string and the dot is also calculated]

and:

B4X:
Dim x As Double
Dim y,j As Int
Dim z As String

x= 12.689
   Log (x) 'gives 12.869
y=x
   Log(y) 'gives 12
   
z=NumberFormat(x,0,0)
   Log(z) 'gives 13
   
j=z.Length
   Log(j) 'gives 2 [correct]

P.S. You cited 'y', but it seems you never used it.
I've tried to translate what you said. It was maybe better if you posted your code
 
Last edited:
Upvote 0

DarkMann

Member
Licensed User
Longtime User
If you just need to find the number of integer digits in the number you could try just taking its base-10 logarithm and adding 1.

B4X:
Dim numb as Double
Dim size as Int

numb=12345.67

size=Floor(Logarithm(numb,10))+1

You may not need the Floor statement, but I like to be safe. Also, taking a Logarithm may be slower than other methods as it is a complex math function.

David
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You can use the code below if your numbers are always positive:
B4X:
Dim x As Double
Dim n As Int
x = 9980.76
n = Floor(Logarithm(x, 10)) + 1
Log(n) ' gives 4
    
x = 12.689
n = Floor(Logarithm(x, 10)) + 1
Log(n) ' gives 2
If the numbers can also be negative. use the following code:
B4X:
x = -9980.76
n = Floor(Logarithm(Abs(x), 10)) + 2
Log(n) ' gives 5
which does include the minus sign.

The length of the integer part of a number is the integer part of the logarythm, base, 10 of the number plus 1.

In your example you should have used:
B4X:
Dim x As Double
Dim n As Int
Dim txt As String

x = 9980.76
txt = NumberFormat2(x, 0,0,0,False)
n = txt.Length
Log(n)
False to avoid the grouping.

Best regards.

EDIT: DarkMan was faster then I.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Thank you for the tips and different options. Below is the final code that gave me the result I am seeking:

Dim lmreadpr As Int
Dim txtmreadpr As String
Dim mreadpr, mread as double

txtmreadpr = NumberFormat2(mreadpr, 0,0,0,False) 'integer of mreadpr
lmreadpr=txtmreadpr.Length 'length of integer of mreadpr
'Use if statements based on length of txtmreadpr
If lmreadpr = 2 Then 'exple:txtmreadpr =99
mread = mread + 100
Else If lmreadpr = 3 Then 'exple:txtmreadpr =998
mread = mread + 1000
else if........
..
End if
 
Upvote 0
Top