LastIndexOf acting same as IndexOf ????

BasicBert

Member
Licensed User
Longtime User
Hello all.

:sign0085:
I'm having trouble using the LastIndexOf and IndexOf functions in my app.
So to test what's happening, I wrote a simple testprogram that looks like :

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
Dim Amount As String
Dim pos As Int : pos = 0
End Sub

Sub Activity_Create(FirstTime As Boolean)
Amount = "12"
UseLastIndexOf
UseIndexOf
Amount = "123.4"
UseLastIndexOf
UseIndexOf
Amount = "1234.56"
UseLastIndexOf
UseIndexOf
Amount = "12345.67"
UseLastIndexOf
UseIndexOf
Amount = "123456.789"
UseLastIndexOf
UseIndexOf
End Sub
Sub UseLastIndexOf
   Log("Amount = " & Amount)
   pos = Amount.LastIndexOf(".")
   Log("LastIndexOf pos = " & pos)
   Log(" ")
End Sub
Sub UseIndexOf
   Log("Amount = " & Amount)
   pos = Amount.IndexOf(".")
   Log("IndexOf pos = " & pos)
   Log(" ")
End Sub

Sub Activity_Resume

End Sub

Now what I expected was that IndexOf would give other results then LastIndexOf would give me depending of the position of the "." in the Amount-string, but my tests show the same results for both :

Amount = 12
LastIndexOf pos = -1

Amount = 12
IndexOf pos = -1

Amount = 123.4
LastIndexOf pos = 3

Amount = 123.4
IndexOf pos = 3

Amount = 1234.56
LastIndexOf pos = 4

Amount = 1234.56
IndexOf pos = 4

Amount = 12345.67
LastIndexOf pos = 5

Amount = 12345.67
IndexOf pos = 5

Amount = 123456.789
LastIndexOf pos = 6

Amount = 123456.789
IndexOf pos = 6

Can anyone tell me what I do wrong or whatever is happening?

What I really like to use, but can't find anywhere is a function like in VB or VBA to format numerical strings, like the old fashioned print using "###.##"
 

klaus

Expert
Licensed User
Longtime User
What do you expect ?
IndexOf gives the position of the first occurance.
LastIndexOf gives the position of the last occurance.
In your case the dot is only one time in your string so the first and last occurances are the same !

Best regards.
 
Upvote 0

BasicBert

Member
Licensed User
Longtime User
What do you expect ?
IndexOf gives the position of the first occurance.
LastIndexOf gives the position of the last occurance.
In your case the dot is only one time in your string so the first and last occurances are the same !

Best regards.
Klaus,

How quick can you answer? Speed of light ???

If I look at it the way you describe it, I see why the results are the same.

What I was expecting is that the LastIndexOf would tell me at what position from the end of the string the "." would be.

What I was looking for to do is :
- determine how many fractions are within the Amount
- add extra 0's untill I get 2 fractions
- delete extra fractions after the second

With IndexOf I can determine how may integers are before the dot and put a number of spaces before that to align the amount as I wish.
 
Upvote 0

BasicBert

Member
Licensed User
Longtime User
I tried NumberFormat, but not as extensively as I did with IndexOf. As I understand it chops of extra fractions, but does not add them if necessary.
And I don't want the extra comma.

Any suggestion for aligning a bunch of amounts at the periods like :

12.34
123.45
1234.56

and adding zeroes after the period to get 2 fractions?

Thanks in advance.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Perhaps you can try something like this (I haven't test it,so careful for errors):
B4X:
Dim a As Double 
a=12.4
Dim b As String 
b=Round(100*a)
b=b.SubString2 (0,b.Length -2) & "." & b.SubString (b.Length -2)
'And if you want the extra zeros in front, you could write:
'b=NumberFormat(b.SubString2 (0,b.Length -2),4,0) & "." & b.SubString (b.Length -2)
Msgbox(b,"ok")
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Is this method shown below what you are looking for?
B4X:
Dim a() As String 
a=Array As String(123456.789,12.4,1234.56,12.3478,123.45)
For i=0 To 4
   a(i)=NumberFormat2(a(i),0,2,2,False)
Next
Msgbox(a(0) & CRLF & a(1) & CRLF & a(2) & CRLF & a(3) & CRLF & a(4) & CRLF ,"Summary")
Summary of results:
123456.79
12.40
1234.56
12.35
123.45
 
Upvote 0

BasicBert

Member
Licensed User
Longtime User
Thanks for the replies.
I will try both methods tonight.

@Maharis : this looks great for the part of the fractions (2), but now I'm still looking to align the decimal points by padding spaces to the left instead of zeros. But I think I can figure that out.

I'll report back as soon as I have tested things out tonight.
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
To right align the numbers why not put labels with gravity set to RIGHT onto the layout.

Then place your formatted numbers onto the labels.

Regards, Ricky
 
Upvote 0

BasicBert

Member
Licensed User
Longtime User
Hi guys,

A combination of the tips from Mahares and Ricky D seem to be the easiest way.
Especially Ricky D's suggestion is too easy to think off, but soooo simple and convenient :icon_clap:.
Since the period will always be followed by exact 2 fractions, the alignment will be OK automatically :sign0060:.
Maybe just for these amounts I might use a fixed font if necessary.

I just can't wait to get back home from work to try everything out!
Thanks to all.
 
Upvote 0

BasicBert

Member
Licensed User
Longtime User
:sign0013: for replying so late.
As stated earlier, the moment I wanted to test your replies my PC broke down.
Bought a new one, but it takes a week to get a new PC to do everything the way I want it.

So finally yesterday I could do some tests.

As expected, RickyD's solution worked perfectly just by its simplicity! Right aligning does most of the work.
In combination with a bit of Maheres' code I'm satisfied :
B4X:
lblPrice.Text=NumberFormat2(lblPrice.Text,1,2,2,False)
takes care of padding to 2 decimals and putting a 0 in front of the decimals if not already something there.

So,

1.0 becomes 1.00
2 becomes 2.00
.3 becomes 0.30
40 becomes 40.00
etc.

Now up to the next challenge!;)
 
Upvote 0

johnB

Active Member
Licensed User
Longtime User
Mahares, brilliant solution, 1 line of code.
I agree with BasicBert. lastindexof is very confusing and should probably (i'm no English scholar) be re written to say

"Returns the index of the last occurrence of SearchFor string, (following not necessary) - indexed from the beginning of the string"
It's irrelevant and confusing saying that it searches from the front or the back

Keep up the good work on the forum, it's a great help
 
Upvote 0
Top