a routine to convert dollars to a particular length string.
i could not find a another routine and/or faster code to do this, it would have been nice to find code to do this. i did not post the code in the source code section in the case somebody else had a better or faster way.
after David Roberts posted his code to convert a number to a string, i was able to use his method to create the main part of building the string.
the reason for the code was that i wanted something faster than what i could have created using a predefined compiler function and also i wanted a function to return a blank string as an option if the amount was equal to zero.
the program returns a string filled with the percent(%) characters if there are is not enough room in the string for the full length of the number.
the returned string has to be at least 4 characters long for all numbers, such as .01 will respond to "0.01", because that at least a zero dollar digit will be be returned.
the program has a way to return a blank string if the amount is equal to 0.00(zero).
an alteration of David Roberts code on converting numbers to strings is used here.
if somebody can increases the speed of this program, please do so.
i could not find a another routine and/or faster code to do this, it would have been nice to find code to do this. i did not post the code in the source code section in the case somebody else had a better or faster way.
after David Roberts posted his code to convert a number to a string, i was able to use his method to create the main part of building the string.
the reason for the code was that i wanted something faster than what i could have created using a predefined compiler function and also i wanted a function to return a blank string as an option if the amount was equal to zero.
the program returns a string filled with the percent(%) characters if there are is not enough room in the string for the full length of the number.
the returned string has to be at least 4 characters long for all numbers, such as .01 will respond to "0.01", because that at least a zero dollar digit will be be returned.
the program has a way to return a blank string if the amount is equal to 0.00(zero).
an alteration of David Roberts code on converting numbers to strings is used here.
if somebody can increases the speed of this program, please do so.
Code:
'dollar2string 'pbcc 4.04 ' #COMPILE EXE #DIM ALL #REGISTER NONE FUNCTION dollar2string( temp1 AS EXT,TEMP2 AS LONG,zerostringblank AS LONG) AS STRING REGISTER digit AS LONG REGISTER digitinthestring AS LONG REGISTER nextdigit AS LONG LOCAL countdigit AS LONG LOCAL remains AS QUAD LOCAL ptrss AS BYTE PTR LOCAL negative AS LONG LOCAL ss AS STRING IF TEMP2<4& THEN ss=STRING$(TEMP2,"%"):GOTO EXITTHEFUNCTION ss=SPACE$(TEMP2) IF temp1<0 THEN remains=ABS(TEMP1*100) negative=1 ELSE remains=TEMP1*100 END IF IF remains=0 THEN IF zerostringblank THEN GOTO exitthefunction ptrss=STRPTR(ss) POKE BYTE,ptrss+temp2-1,48 POKE BYTE,ptrss+temp2-2,48 POKE BYTE,ptrss+temp2-3,46 POKE BYTE,ptrss+temp2-4,48 GOTO EXITTHEFUNCTION END IF ptrss=STRPTR(ss) POKE BYTE,ptrss+temp2-1,48 POKE BYTE,ptrss+temp2-2,48 POKE BYTE,ptrss+temp2-3,46 POKE BYTE,ptrss+temp2-4,48 IF remains>99999 THEN IF temp2<7 THEN ss=STRING$(TEMP2,"%"):GOTO EXITTHEFUNCTION POKE BYTE,ptrss+temp2-7,44 IF remains>99999999 THEN IF temp2<11 THEN ss=STRING$(TEMP2,"%"):GOTO EXITTHEFUNCTION POKE BYTE,ptrss+temp2-11,44 IF remains>99999999999 THEN IF temp2<15 THEN ss=STRING$(TEMP2,"%"):GOTO EXITTHEFUNCTION POKE BYTE,ptrss+temp2-15,44 IF remains>99999999999999 THEN IF temp2<19 THEN ss=STRING$(TEMP2,"%"):GOTO EXITTHEFUNCTION POKE BYTE,ptrss+temp2-19,44 END IF END IF END IF END IF countdigit=1 FOR digit = TEMP2 TO 1 STEP -1 NextDigit=Remains MOD 10 digitinthestring=digit-(countdigit\3) IF digitinthestring=0 THEN ss=STRING$(TEMP2,"%"):GOTO EXITTHEFUNCTION ASC(ss,digitinthestring) = Nextdigit+48 Remains=Remains \ 10 IF remains=0 THEN EXIT FOR INCR countdigit NEXT IF negative=0 THEN EXITTHEFUNCTION FOR negative=digit-(countdigit\3) TO 0 STEP -1 IF PEEK(BYTE,ptrss+negative)=32 THEN POKE BYTE,ptrss+negative,45 GOTO exitthefunction END IF NEXT ss=STRING$(TEMP2,"%"):GOTO EXITTHEFUNCTION exitthefunction: FUNCTION=SS END FUNCTION '--------------------------------------------------------------------------- FUNCTION PBMAIN ( ) AS LONG LOCAL K AS EXT 'limits for accuracy 'lower -9999999999999.99 'uppper 9999999999999.99 K= 9999999999999.99 PRINT k PRINT dollar2string(K,25,0) K=-9999.99 PRINT k PRINT dollar2string(K,25,0) k=-100000000.00 PRINT k PRINT dollar2string(K,25,0) K=1.99 PRINT dollar2string(K,25,1) K=11111.99 PRINT dollar2string(K,5,1) PRINT "there is not enough length in string to place then number " k=.01 PRINT dollar2string(K,3,1) k=-.00 PRINT k PRINT dollar2string(K,9,0) k=-.00 PRINT k PRINT dollar2string(K,1,1) PRINT "the last string is filled with spaces because of an option in the funciton" WAITKEY$ END FUNCTION
Comment