News:

Please note new Forum URL: forum.designcad.com

Main Menu
DesignCAD 3D Max 2027 box
Now available

DesignCAD 2027

Hatches that follow their boundaries, an on-screen dial to turn your model, and a way to find any command by name, in a clean Windows 11 look.

removing leading zeros

Started by bobarthur, February 02, 2012, 06:59:25 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

bobarthur

Thanks to this forum I am able to use a macro to convert a number to metric and print it out.  I am converting a lot of numbers that are less than 1.  I do not wish to print out the leading zero.  0.18 needs to be .18.  Can this be done?

Lar

#1
It can be done, but with a lot of code:

Look in the macros help file for string functions (not sys$()...) that will allow you to do what I'm about to describe: <a> You will have to convert the numeric value into text, <b> count the amount of characters in the text string, <c> let the macro find the amount of characters from the left to the first period (.), <d> deduct c from b, which gives you the amount of characters from the period to the right end, <e> get the d amount of characters from the left and put it in a string variable (don't change the text back into numeric value because that will put the zero back). <f> print e to the file you would have opened earlier in the macro.

Have fun.

...Or, if all your numbers start with "0." you could just print the values to a text file and use the find and replace command to replace "0." with "."

Lar

DrollTroll

I'd probably use INSTR() to find where the decimal point is, then create substrings of the left (exluding decimal) and right (including decimal) portions. If the value of the left portion is zero, your right portion becomes the entire new string. If it's not, keep the original string as-is.
25 years with DesignCAD

bobarthur

Thanx for the info.  I was afraid that it was going to take this kind of code.  I'll just have do it when I have lots of time. thanx

Lar

It sounds daunting but don't let that stop you. One day writing the code = an eternity using the code.

As for my second bit of advice above: if you let the macro generate a space before the numbers and write to a text file you can open the text file and replace all of the "space-zero-period" (" 0.") with "period".

Lar

MarkX

If BasicCad had an IIF() function it might be possible to do this all as one function call. But since you have to call subroutine anyways, might as well make the writing easy.



'Trim_Leading_Zeroes_MAS_v01.d3m
'Put the number you want to have frontal zeroes trimmed from
'into variable trimMe$. Then GOSUB TrimMe
'Nothing happens to trimMe$ if there are no frontal zeros
'Doesn't check that this isn't a decimal number, but
'frontal zeroes zapped in all cases.

'Examples
trimMe$ = "0.1234"
GOSUB TrimMe
MESSAGE trimMe$

trimMe$ = "00.8956L"
GOSUB TrimMe
MESSAGE trimMe$

END

'----------------------------------------------------------------
TrimMe:
llZero = 0
FOR lnTM = 1 TO LEN(trimMe$)
'MESSAGE MID$(trimMe$,lnTM,1)
   IF MID$(trimMe$,lnTM,1) = "0" THEN
      llZero = 1
   ELSE
      EXIT FOR
   END IF
NEXT lnTM
IF llZero = 1 THEN trimMe$ = RIGHT$(trimMe$,LEN(trimMe$)-lnTM+1)

RETURN