Can anybody help?
I have the following four lines of code to format any number between 1 and 9999
Console.WriteLine(Format(1, " 0 " + "TEST"))
Console.WriteLine(Format(12, " 0 " + "TEST"))
Console.WriteLine(Format(123, " 0 " + "TEST"))
Console.WriteLine(Format(1234, "0 " + "TEST"))
The output will be:
1 TEST 12 TEST 123 TEST 1234 TEST
Does anybody know how I can combine the above four lines into a single line of code whereby the variable can be any number between 1 and 9999?
Many thanks in advance!
Darren
Convert number to string, count how many characters in string.
Add correct number of space in front.
Hi @isocam. This is just a tip about part of your last question about generating a random Integer between 1 and 9999. You can use the vb.net VBMath Class, and its two methods (Randomize & Rnd) to do that. The following basically a copy of the example on their documentation website that can been slightly customized for use in an iLogic rule and constrained to the values you specified.
VBMath.Randomize
For i As Integer = 1 To 10
Dim iVal As Integer = CInt(Int((9999 * VBMath.Rnd) + 1))
Logger.Info("iVal = {0}", Format(iVal, "0000"))
Next
To see the 10 resulting values after running this iLogic rule, just look within your iLogic Log window. Below is an example of a result I got from it.
INFO| 16: >>---------------------------
INFO|iVal = 4726
INFO|iVal = 1023
INFO|iVal = 1380
INFO|iVal = 1941
INFO|iVal = 7234
INFO|iVal = 4799
INFO|iVal = 8646
INFO|iVal = 6852
INFO|iVal = 4866
INFO|iVal = 7308
Edit: I just updated the code example to include the "{0}" and the Format(iVal, "0000"), portion, just to help with the leading zero's part of your request, since that seemed to be the primary issue here. Now, when it generates an Integer with a value of 999 or less, there will be leading zero's filling in the correct number of digits to ensure a 4 digit number. However, that Format method results in a String, not an Integer, so to use the resulting value as an Integer, you would have to use a data type conversion method (like CInt() or Convert.ToInt32) on it first.
Wesley Crihfield
(Not an Autodesk Employee)
If you want to add whitespaces at the left side of string, you can use PadLeft function of string object
For Each i In {1, 12, 123, 1234}
Dim msg As String = i.ToString().PadLeft(4)
Logger.Debug(msg)
next
Result
DEBUG| 1
DEBUG| 12
DEBUG| 123
DEBUG|1234
Can't find what you're looking for? Ask the community or share your knowledge.