Is there a faster way to do this? - format numbers and fill listbox

i have a method to format currency numbers

[code]Public Function Get_CurrencyAsFormattedString(nro_original as currency=0.0) as string

dim cadena_formato as string
cadena_formato = “###,###,###,###.00;-###,###,###,###.00”
return format(nro_original, cadena_formato)

End Function[/code]

when i format A LOT of numbers (1.419.818), i takes 21335 miliseconds as i see in my Profile Data

Is there a faster way to do this?

windows 10 i3 8gb ram // xojo 2016R3

Changing the format to a constant might help a bit, but you might just be seeing the overhead of calling a function.

What if you put the format in a global constant and call Format directly?

Untested, but maybe adding the numbers to a database table and updating the values using a SQL statement ?

eg one field is the number, another field is the text version.
Fill the number field, then

update  tablename set textfield = printf("%,.2f", floatField)

Might work
The first parameter is the formatting string

ok… i will try and let you know the results… thanks kem and jeff

Do you think using:

nro_original.ToText(locale)

will be slower, faster or the same?

The problem is that it will show a symbol ($).

I did a test, changing from nro_original.ToText(locale) to Format(nro_original,cadena_formato), part of the code:

For i = 1 To 1419818 nro_original = i texto = nro_original.ToText(locale) 'texto = Format(nro_original,cadena_formato) Next i
Using format I get 650 ticks difference.
Using .ToText(locale) I get 247 ticks difference.
I didn’t use a function, maybe will test that too.

Out if curiosity…

Why would you want to display 1.4 miilln values ?

Calling GetCurrencyFormatEx directly should be quicker than going via the framework as you would be able to re-use certain things that the framework would need to initialise for every call such as the CURRENCYFMT structure and the the buffer which receives the output.

roman: i am doing a stress test with a listbox that has 300.000 rows and 5 numeric columns. my final goal is to populate the listbox faster as possible

If you have a lot of data like this, you usually only work on the data that is currently shown to the user, maybe 100 rows. There’s not much point formatting all the other data if the user never actually sees it.

I’m going to think outside of the box here. The best way to reduce the overhead may be to do the work ahead of time. Add a column to the database and store a copy of the currency field as a pre-formatted string. When milliseconds count and you’re displaying 300K records, this might be the way to speed up the initial hit.

If I was designing a program populating 300,000 rows, I would not do it all in one method.

I would probably populate like 5 or ten pages, which can be done fairly fast, and then use a timer to populate the rest of the rows. For the user, the update will be immediate or so, he will even be able to scroll down a bit, but the completion will take somewhat longer. Since it is done in a timer, the UI will not be frozen, and it should pretty much transparent to the user.

Julian suggests the same thing above.

Indeed, excellent way to cut processing time :slight_smile:

If you’re putting values in a listbox then I would put them in the celltag and only implement celltextpaint to draw the number formatted as you want - that way you only do this for the cells in the listbox that are visible and not every row. That could cut down the number of times you need to do this by a lot.

+1, nah, +100!

There are several benefits connected to this:
– You can fill the listbox much faster. Instead of calculating strings for each row, filling the listbox is just a combination of empty AddRows and AddRowTags (or AddCellTags).
– You have almost full flexibility about the design of your cell: multiple lines, images, fonts and sizes can be created on the fly in the draw events.

This is almost as good as having direct access to the DataSource object.

Btw, this “###,###,###,###.00;-###,###,###,###.00” is redundant.
Doing this “-###,###,###,###.00” is enough due positive numbers don’t show the sign.

thanks all for the ideas. i think jason parsley approach is the way to go. and thanks massimo valle for your recommendation

there is not a faster way to format number, as far as i know, but the provided solution helps a lot showing values in the listbox