Bug? Bug in NumberFormat2?

Computersmith64

Well-Known Member
Licensed User
Longtime User
Not sure if this is a bug or not, but here's what I found:

I have a sub that loads a record from a database. The sub is pretty standard & has a query to get the record I'm looking for & then loads the various fields into views. In the process of doing this, it formats a couple of numbers (an appttime field into a ##:## format & a cost field into a #.## format). The first formatting looks like this:

Code: Copy
txtApptTime.Text = $"$2{DateTime.GetHour(mRec.get("appttime"))}:$2{DateTime.GetMinute(mRec.get("appttime"))}"$

& the second formatting looks like this:

Code: Copy
txtCost.Text = NumberFormat2(mRec.Get("cost"), 1, 2, 2, False)

(Don't ask me why I use 2 different ways of formatting - I just do)

After the activity loads & the first time I call this sub to load a record, it all works fine. Eg: if the time being formatted is 11:45AM, then it will be shown as "11:45". However, the second & subsequent times I call this sub, the time will be shown as "11.00:45.00". If I comment out the second formatting line (the one that uses NumberFormat2) then it all works fine. Also, if I change the first formatting line to:

Code: Copy
txtApptTime.Text = NumberFormat(DateTime.GetHour(mRec.get("appttime")), 2, 0) & ":" & NumberFormat(DateTime.GetMinute(mRec.get("appttime")), 2, 0)

then it all works fine as well.

It seems that the NumberFormat2 is somehow creating some persistent format that is overriding the $"$2... "$ formatting I am trying to use. It's easy enough to work around by just using NumberFormat instead of the way I initially did it, but still seems odd to me.

- Colin.
 
It seems that the problem is in the first statement (time) not in the second (Cost).
Try to use the old style method instead of smart string, PLEASE ;)
Hi Luca - I don't think that it is the smart formatting that's the issue. If I comment out the NumberFormat2 line, then the smart formatting works fine. Also, I was using the smart formatting without any issues for a long time before I added the Cost field & the NumberFormat2 formatting for it.

I use smart formatting in other apps without any issues too.

- Colin.
 
That the problem is in the smart string, of course
Run it again - except this time uncomment the smart formatting statement for the time & comment the one below it. Tap the Test button twice & you'll see that the smart formatting gets screwed up the second time. Now comment out the NumberFormat2 line & run it again. Tap the Test button as many times as you want & you'll see that the smart formatting is correct every time.

This tells me that the NumberFormat2 statement is doing something that is affecting the smart formatting.

- Colin.
 

Attachments

The correct code is:
Code: Copy
Log($"$2.0{iHour}:$2.0{iMinute}"$)

Your code doesn't put any limit on the maximum number of fractions.
See the number formatter section: https://www.b4x.com/android/forum/threads/b4x-smart-string-literal.50135/#content

The inconsistency is indeed strange here. However the correct way to fix it is by explicitly setting the format.
Thanks Erel - it's weird that it's only a problem after a call to NumberFormat2. In code where I've never made that call, I don't have an issue with the way I've written the format - but I'll make sure I follow the format above in future. Having said that, I find it strange that you need to specify fractions on an integer. I could understand it if the number being formatted was a double or float, but given that it's a whole number I would have expected it to just return a 2 digit format of it.

- Colin.
 
Back
Top