iLogic Format a Number

iLogic Format a Number

pball
Mentor Mentor
7,080 Views
13 Replies
Message 1 of 14

iLogic Format a Number

pball
Mentor
Mentor

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

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Accepted solutions (1)
7,081 Views
13 Replies
Replies (13)
Message 2 of 14

Curtis_Waguespack
Consultant
Consultant

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

EESignature

0 Likes
Message 3 of 14

pball
Mentor
Mentor
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'.
Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 4 of 14

Curtis_Waguespack
Consultant
Consultant

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

EESignature

0 Likes
Message 5 of 14

pball
Mentor
Mentor
doesn't change a thing.
Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 6 of 14

Anonymous
Not applicable

have you tried round?

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

 

or

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

0 Likes
Message 7 of 14

pball
Mentor
Mentor
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.
Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 8 of 14

Anonymous
Not applicable

this works in Inventor 2012 ilogic

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

0 Likes
Message 9 of 14

ekinsb
Alumni
Alumni
Accepted solution

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
Mentor
Mentor

Deleted.

 

Edit:

 

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

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 11 of 14

llorden4
Collaborator
Collaborator

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
0 Likes
Message 12 of 14

llorden4
Collaborator
Collaborator

@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
0 Likes
Message 13 of 14

JBerns
Advisor
Advisor

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 AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 14 of 14

rossano_praderi
Collaborator
Collaborator

@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!
---------------
0 Likes