konijn
Junior Member
Posts: 96
|
Post by konijn on Jul 3, 2020 5:19:54 GMT -5
|
|
|
Post by tenochtitlanuk on Jul 3, 2020 9:03:58 GMT -5
Well done indeed!
|
|
konijn
Junior Member
Posts: 96
|
Post by konijn on Jul 25, 2020 12:51:17 GMT -5
How can I remove a zerro in front of the cow number?
0040 must be 40
0118 must be 118
1250 must be 1250 (the same)
Thanks!
|
|
|
Post by tenochtitlanuk on Jul 25, 2020 13:40:45 GMT -5
Something like this??
data "0040", "0118", "1250"
for i =1 to 3 read num$ print "|"; num$; "|", "|"; stripLeadingZeros$( num$); "|" next i
end
function stripLeadingZeros$( i$) flag =0 for i =1 to len( i$) ch$ =mid$( i$, i, 1) if ch$ <>"0" then flag =1 if flag =1 then stripLeadingZeros$ =stripLeadingZeros$ +ch$ else stripLeadingZeros$ =stripLeadingZeros$ +" " end if next i end function
Result ..using vertical bar symbol to make the result clearer..
|0040| | 40| |0118| | 118| |1250| |1250|
|
|
konijn
Junior Member
Posts: 96
|
Post by konijn on Jul 25, 2020 14:09:12 GMT -5
Thanks!!
New challenge in the program
|
|
|
Post by meerkat on Jul 25, 2020 18:48:47 GMT -5
Or maybe something like this:
data "0040", "0118", "1250"
for i =1 to 3 read num$ num = val(num$) print "|"; num$; "|", "|"; num; "|";using("####",num) next i
|
|
|
Post by Brandon Parker on Jul 25, 2020 20:08:18 GMT -5
Or ... You can do it with one simple line ... var$ = "0040" Print var$
'Conversion to a numeric type removes 'all leading zeros automatically 'Then just convert it right back to a string var$ = str$(Val(var$)) Print var$ {:0) Brandon Parker
|
|
|
Post by tenochtitlanuk on Jul 26, 2020 3:51:05 GMT -5
On occasions I've wanted this kind of formatting I usually wanted to maintain right-justified numbers in tabbed columns, hence kept the leading blanks. So solution depends on OP's needs...
|
|
|
Post by meerkat on Jul 26, 2020 9:55:44 GMT -5
maybe something like this would work?
data "0040", "0118", "1250","0001","0010","0100" for i =1 to 6 read num$ num$ = using("#####",val(num$)) print num$;"|", next i
Or simply print using("#####",val(num$));"|"
|
|
|
Post by Brandon Parker on Jul 26, 2020 19:50:13 GMT -5
This can still be done with a one-liner (no loops ... yay for efficiency), and not limited by hard-coding the number of "#" characters in a Using() function. Of course, you can do the same thing with the Using() function if you get creative (see example below for both ways).
var1$ = "0040" Print var1$ var2$ = "1250" Print var2$ Print
'Conversion to a numeric type removes 'all leading zeros automatically 'Then just convert it right back to a string Print Space$(Len(var1$) - Len(str$(Val(var1$))));str$(Val(var1$)) Print Space$(Len(var2$) - Len(str$(Val(var2$))));str$(Val(var2$))
Print
'Now with the Using() function Print Using(ReplStr$(Space$(Len(var1$)), " ", "#"), Val(var1$)) Print Using(ReplStr$(Space$(Len(var2$)), " ", "#"), Val(var2$))
{:0)
Brandon Parker
|
|