Adding leading zero tp inches

Adding leading zero tp inches

Anonymous
Not applicable
514 Views
2 Replies
Message 1 of 3

Adding leading zero tp inches

Anonymous
Not applicable

Hi guys,

 

I'm trying to format dimensions to have a leading zero in front of the inches. This is the code I have so far.

 

 

Dim DecimalDimension_W As Decimal = WIDTH
Dim feet_W As Integer = Floor(DecimalDimension_W)\ 12
Dim inches_W As String = RoundToFraction((DecimalDimension_W - (feet_W * 12)), 1/16, RoundingMethod.Round)
If (feet_W >= 1) Then
Part_Width = String.Format("{0}'-{1}""", feet_W, inches_W)
Else
  inches_WNofeet_W = RoundToFraction((DecimalDimension_W), 1/16, RoundingMethod.Round)
 Part_Width = String.Format("{0}""", inches_WNofeet_W)
End If

Any suggestions? 

0 Likes
515 Views
2 Replies
Replies (2)
Message 2 of 3

sgwilliams
Collaborator
Collaborator

When you originally convert a number to a string, you use String.Format to add leading zeros. I see that you have already used this but the formatting is wrong it should look like this.

 

myString = String.Format("{0:0000}", myInteger)

 

Will return a string with a least 4 digits, including leading zeros.

 

You might also try

 

myString = myString.PadLeft(desiredLength, "0"c)
Work Station : Custom built, Liquid Cooled, Asus Pro WS X570-ACE Motherboard; AMD Ryzen 9 3900X 3.8 GHz 12-Core Processor; ASUS TUF Gaming NVIDIA GeForce RTX 3060 V2 OC Edition Video Card; 32 GB CORSAIR Dominator Platinum DDR4-2132 Memory ; Samsung 850 EVO 1TB SSD Drive.
0 Likes
Message 3 of 3

sgwilliams
Collaborator
Collaborator

@Anonymous : Ignore the last reply I posted it without testing it. This is tested and works.

 

 

Imports System Globalization
Dim var as Double
var = .0023
Console.WriteLine(var.ToString("G", CultureInfo.InvariantCulture)) ' Displays 0.0023

 Take note this was tested in Visual Studio.NET  2015

Work Station : Custom built, Liquid Cooled, Asus Pro WS X570-ACE Motherboard; AMD Ryzen 9 3900X 3.8 GHz 12-Core Processor; ASUS TUF Gaming NVIDIA GeForce RTX 3060 V2 OC Edition Video Card; 32 GB CORSAIR Dominator Platinum DDR4-2132 Memory ; Samsung 850 EVO 1TB SSD Drive.
0 Likes