Page 1 of 1

String question. Inserting Commas at positions.

PostPosted: Tue Apr 25, 2006 2:13 am
by domino
My pub generates some numbers from a database.

The generated number range is between 1000 and 100 000 000

The larger numbers are difficult for the user to quickly interpret due to the absence of commas separating the millions and thousands.

I'd like to be able to display the final results with commas inserted in the appropriate positions. (There are no decimal places to consider) and the modified results are for display only they will not be required for further computational purposes.

eg 500000 500,000
10320654 10,320,654
119567398 119,567,398


I`ve had some minor success with splitting the strings into groups and inserting the commas as another string... but my brains beginning to hurt with a plethora of variable names.... Is there is smart way to do this..

Cheers
Dave

PostPosted: Tue Apr 25, 2006 4:47 am
by Cipolla
Hello Dave,

i remember there have been a plugin to convert a given number with seperators. I mean it was called "NeoFormatNum" and i think it was from a spanish developer.

Maybe someone else can help. I could not find it in the moment.

PostPosted: Tue Apr 25, 2006 4:49 am
by Cipolla
Damn, a quick forum search, and here it is:

http://www.neosoftware.com/forum/viewtopic.php?t=13486&highlight=format+number

First think (or search), then reply :oops:

PostPosted: Tue Apr 25, 2006 6:16 am
by domino
Thanks Klaus (and David)

I was hoping to avoid a plugin just for this task....

If there is a programmatic way to do this I would prefer it.. If not then I`ll follow the plugin route....

Cheers
Dave

PostPosted: Tue Apr 25, 2006 6:25 am
by HPW
You should be able to 'StrLen' your number and loop through it and insert your delimiter.

PostPosted: Tue Apr 25, 2006 6:31 am
by Gaev
Dave:

a) Calling code set ...
Code: Select all
SetVar "[TheNumbers]" "[whatever variable with comma-less numbers]"
GoSub "SeparateTheNumbers"
... on return [TheNumbers] will have the desired values

b) The SubRoutine ...
Code: Select all
:SeparateTheNumbers
... for integers upto 999 999 999

SetVar "[SeparatorCharacter]" ","

StrLen "![TheNumbers]" "[SizeOfTheNumbers]"
SetVar "[FirstLen]" "[SizeOfTheNumbers]-2"
SetVar "[SecondLen]" "[SizeOfTheNumbers]-5"

If "[FirstLen]" ">" "1"
   StrIns "[SeparatorCharacter]" "[TheNumbers]" "[FirstLen]" "[TheNumbers]"
EndIf

If "[SecondLen]" ">" "1"
   StrIns "[SeparatorCharacter]" "[TheNumbers]" "[SecondLen]" "[TheNumbers]"
EndIf

Return



c) If you have a lot of such conversions, instead of a GoSub, you might consider ...
Code: Select all
Call "SeparateTheNumbers" "[whatever variable with comma-less numbers]"


d) For your (continental) European clients, you might even consider passing the delimiter (comma or decimal point) as another parameter ...
Code: Select all
Call "SeparateTheNumbers" "[whatever variable with comma-less numbers]" "[SeparatorCharacter]"

PostPosted: Tue Apr 25, 2006 7:28 am
by Wrangler
Here's one Sam Cox provided in another post:

Math "0[textentry1]*1" "0" "[temp]"
SetVar "[sofar]" ""
StrLen "[temp]" "[len]"
While "[len]" ">" "3"
SubStr "[temp]" "[len]-2" "3" "[x]"
SubStr "[temp]" "1" "[len]-3" "[temp]"
SetVar "[sofar]" ",[x][sofar]"
SetVar "[len]" "[len]-3"
EndWhile
SetVar "[display]" "[temp][sofar]"

I haven't tested this.

PostPosted: Tue Apr 25, 2006 10:29 am
by domino
Hans...
You should be able to 'StrLen' your number and loop through it and insert your delimiter.


That was the plan.. but I couldn`t make it work....

Gaev..

Thanks.. Your code is a 'runner' and works nicely


Wrangler...

I recall this having been covered before..but couldn`t find the post.. so thanks. I`ll have a play with Sam's code later on.


Cheers
Dave

PostPosted: Tue Apr 25, 2006 9:10 pm
by David de Argentina
Hello Dave,

Take a look of this:

http://specialapps.software-zone.com/fi ... Format.cab

this plugin does exactly you need.

cheers,

David de Argentina

PostPosted: Wed Apr 26, 2006 3:04 am
by domino
Thanks David... I`ve downloaded a copy for future use. But for this particular job I didn`t want to use any additional plugins. Gaev's routine did the trick this time round.

Cheers
Dave