Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

iLogic Format a Number

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
pball
6331 Views, 13 Replies

iLogic Format a Number

I'm trying to format a number with leading and trailing zeros. If I only needed trailing zeros I'd set the number as a parameter and force the formating there. I tried using the Format function but it fails and I found one other instance of someone trying it and it failing also.

 

For example I want to change 2.125 into 02.13

 

P.S.

Just for fun I went to the VBA editor and tried this.

debug.Print format(2.125,"00.00")
02.13

 

Works perfectly in the VBA but not in iLogic

13 REPLIES 13
Message 2 of 14
Curtis_Waguespack
in reply to: pball

Hi pball,

 

You can use this:

 

myNumber = format(2.125,"00.00")
MessageBox.Show(myNumber, "iLogic")

 Or just this:

 

MessageBox.Show(format(2.125,"00.00"), "iLogic")

 I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

Message 3 of 14
pball
in reply to: Curtis_Waguespack

The format command does not work in ilogic for me. Also contrary to what I said about it working in VBA it wouldn't work inside a function, it only worked if I used it in the immediate window.

Error on Line 5 : Argument not specified for parameter 'format' of 'Public Shared Function Format(enumType As System.Type, value As Object, format As String) As String'.
Error on Line 5 : Value of type 'Double' cannot be converted to 'System.Type'.
Message 4 of 14
Curtis_Waguespack
in reply to: pball

Hi pball,

 

Does adding a DoubleForEquals line help? :

 

iLogicOption DoubleForEquals Off
myNumber = format(2.125,"00.00")
MessageBox.Show(myNumber, "iLogic")

 I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

Message 5 of 14
pball
in reply to: Curtis_Waguespack

doesn't change a thing.
Message 6 of 14
Anonymous
in reply to: pball

have you tried round?

MessageBox.Show(Round(2.125, 2),"iLogic")

 

or

MessageBox.Show(CStr(Format(2.125, "00.00")))

Message 7 of 14
pball
in reply to: Anonymous

Round by itself doesn't do what I need, the leading 0 in the tens place and the format command throws an error which I pasted a few posts up. So from what I've seen it appears the format() function doesn't work in iLogic.
Message 8 of 14
Anonymous
in reply to: pball

this works in Inventor 2012 ilogic

MessageBox.Show(CStr(Format(2.125, "00.00")))

Message 9 of 14
ekinsb
in reply to: pball

It looks like that for some reason your rule is picking up a different Format function.  The Format function is a leftover from the older Visual Basic, but is still supported in VB.Net for backward compatibility.  You can try specifying the complete namespace to the correct Format function.  An alternative is to use the .Net ToString function to do the formatting.  Both of these are demonstrated below.

 

Dim value As Double = 2.125
MessageBox.Show(Microsoft.VisualBasic.Strings.Format(value,"00.00"), "iLogic")
MessageBox.Show(value.ToString("00.##"), "iLogic 2")

 

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 10 of 14
pball
in reply to: Anonymous

Deleted.

 

Edit:

 

Thanks for the two examples, they both work great and are better solution than my home made function I've been using.

Message 11 of 14
llorden4
in reply to: pball

Awakening this topic as it's the closest post to solving what I'm attempting to do.  I too am trying to format text via iLogic while also concatenating other text to arrive at a lengthy and elaborate self-resolving part number.  I need to utilize a specific amount of characters so I need to convert a number to include preceding zeros as space fillers.  So for example if I have a value of "4" I need to convert to a text value of "004"; I do not currently have a need for a decimal value but wouldn't mind knowing how to apply one.

 

This post covers the concatenating method I am able to succeed with

 

Another post that comes close but doesn't work

 

Some attempts I've tried that have failed include:

SyntaxEditor Code Snippet

Test = Format%d(Width,"000")

Test = Format(Width,"000")

Test = value.ToString("000",Width)

 

Autodesk Inventor Certified Professional
Message 12 of 14
llorden4
in reply to: llorden4


@llorden4 wrote:

Awakening this topic as it's the closest post to solving what I'm attempting to do.  I too am trying to format text via iLogic while also concatenating other text to arrive at a lengthy and elaborate self-resolving part number.  I need to utilize a specific amount of characters so I need to convert a number to include preceding zeros as space fillers.  So for example if I have a value of "4" I need to convert to a text value of "004"; I do not currently have a need for a decimal value but wouldn't mind knowing how to apply one.

 

This post covers the concatenating method I am able to succeed with

 

Another post that comes close but doesn't work

 

Some attempts I've tried that have failed include:

SyntaxEditor Code Snippet

Test = Format%d(Width,"000")

Test = Format(Width,"000")

Test = value.ToString("000",Width)

 


It's only after a post do you think of something.  I was able to achieve my desired results by simply adding by the number of tenths for the desired number of placeholders, converting to a string, and then taking the Right value of the string by the number of desired places.

 

(Example: for 3 decimal places)

Test = Right(CStr(VALUE + 1000),3)

 

This works handily for my condition with no decimal values, would have to include multiple routines to include a decimal format in similar fashion.  If anyone can provide an all inclusive "Format" option for iLogic I'd be all ears.

Autodesk Inventor Certified Professional
Message 13 of 14
JBerns
in reply to: pball

I know this is an old post, but I was trying to format a parameter as a string for use in an iProperty.

 

I had to use this method:

'Declare a shaft diameter single and capture the parameter value
Dim oShaftDiaSingle As Single = Parameter.Value("Shaft_Dia")
' Declare a shaft diameter string
Dim oShaftDia As String = Format(oShaftDiaSingle, "0.0000")

 

All other attempts to format the parameter were unsuccessful. For example, this would not work:

Dim oShaftDia as String = Parameter.Value("Shaft_Dia").ToString("0.0000")

 

Perhaps someone could explain why the "ToString" did not work.

 

Hope this is helpful to others.

 

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using Inventor 2022
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
Message 14 of 14
rossano_praderi
in reply to: JBerns

@JBernsto avoid conversion errors, you have to assign a numeric Type as shown below

 

' First you have to assign a dataType/numericType
Dim oShaft As Single = Parameter.Value("Shaft_Dia")
' OR
' Dim oShaft As Double = Parameter.Value("Shaft_Dia")

MessageBox.Show(oShaft.ToString("0.0000"))
' OR
' MessageBox.Show(Format(oShaft,"0.0000"))

 



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report