Code archives/Miscellaneous/Displaying leading zero's
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
Usage of this function is only for displaying numbers with leading zero's (0) it can be used like this: print addzero$(var,showcount,displayflag=0) returned is a string with the sign and the numbers. var should be any positive or negative number to be displayed. showcount any number from 1 to xxx displayflag can be 0 or 1 behaviour: depending on the showcount (how many numbers shall be shown) it adds 0 to the variable. if showcount=3 and var=5 then the returned variable will be 005 if showcount=3 and var=100 the returned variable will be 100 if showcount=1 then then only the right number of the var will be shown, e.g. var=123 returns 3 if the displayflag is 0 then positive and negative numbers will be without flag ( negative without the - sign) if the number is below 0 (eg -100) and the displayflag is set to 1 or higher , then the display may look like this: -0100 if the number is higher than the showcount, e.g. var=1234, showcount=3, and displayflag is higher than 0 then the output is : +34 displayflag may look like one of following: + < ± ~ (1,2,3,4) see the demo for the every case : | |||||
;==================================================================== ; Project: AddZero ; Version: 1.0 ; Author: Dan ; Email: ~.~ ; Copyright: PD ; Description: Adds leading 0 before numbers so that the number ; 12 will be displayed as 0012 or 00012 depending ; on how many places the display shall be shown ; optional a displaying flag can be set ; (to show the - if the numbers are below) ; or to display a set of customizeabled flags which can be shown ; if the length of the number is higher than it should be displayed ;==================================================================== Function addzero$(var,showcount,displayflag=0) ;add leading 0 to the variable, returns a string (obviously)! ;if the flag is set, it will display the flag ;it displays the -numbers as showcount-1 eg showcount=3, var=3 returns as "-03" ;if showcount is 1 it ignores the - sign and displayflag If showcount<=0 showcount=1 EndIf displayflag=displayflag Mod 5 If var=>0 Select displayflag Case 0 fl$="" Case 1 fl$="+" Case 2 fl$="<" Case 3 fl$="±" Case 4 fl$="~" End Select Else fl$="-" ;Set the flag as - var=-var ;Make the variable as positive number EndIf If Len(var)>showcount If displayflag>0 Txt$=fl$+Right$(var,showcount-1) Else Txt$=Right$(var,showcount) EndIf EndIf If Len(var)<showcount And showcount>1 If fl$="-" And displayflag>0 Txt$=fl$+String("0",showcount-Len(var)-1)+var Else Txt$=String("0",showcount-Len(var))+var EndIf ElseIf Len(var)=showcount And showcount>1 If fl$="-" And displayflag>0 Txt$=fl$+String("0",showcount-Len(var)-2)+Right$(var,showcount-1) Else Txt$=String("0",showcount-Len(var))+Right$(var,showcount) EndIf ElseIf showcount=1 Txt$=Right$(var,1) EndIf Return Txt$ End Function |
Comments
| ||
without the flagFunction addzero$(var, showcount) Return String("-", Sgn(var)<0)+Replace(RSet(Abs(var), showcount)," ","0"); End Function |
| ||
Cool, thanks Bobysait, now i have learned something new ! To adapt your code to display in the way, in which is described above, without extra sign on the + side, use this: (showcout displays the maximum length with the -sign) Function addzero$(var, showcount) If showcount=1 Return Right$(var,1) Else Return String("-", Sgn(var)<0)+Replace(RSet(Abs(var), (showcount-(Sgn(var)<0)))," ","0"); EndIf End Function or do you know the shorter way to do even this ? |
| ||
This seems non intuitive to me the way this function is set up (parameters). We have a language at work with a built in function for inserting leading zeros to a number as a string that makes much more sense to me. I won't post the code here as I don't want to derail this thread but I will post the function's appearance. function zstr$(number#,digitsbeforedecimal%,digitsafterdecimal%) { } I think that gives enough of an idea of how it would work...makes a lot more sense to me. |
| ||
I didnt knew that we are here at the Hells court, and that anyone who posts the code in this forum should be burned to death. Anyway, I needed a function which should display certain ammount of numbers, filled with leading zero's, if the number is lower, but not going over the maximum allowed numbers. This should be the case even if the number goes into negative. And to indicate, that the number is greater than displayed, there was a need for a sign, Which would, aswell, be in the maximum allowed chars. That my posted code isnt as optimal as it could be, is clear, but it is one of the ways to code it. If you dont like it, you do not need to use it. May God bless your souls. |
| ||
Unusual post - I wasn't intending any disrespect or anything just offering an alternative? |
| ||
sorry, maybe i have missunderstood something, my main language is not english. And i do not see where in blitz3d is this zstr$ function. |
| ||
Sorry Dan. There is no zstr function in blitz3d. I was referring to a function we use in my workplace. I hope that makes sense. Language on the internet is tricky especially when you cant see who you are talking to. Easy to be misunderstood. |
| ||
ok thanks. |
Code Archives Forum