Solved

# Add 1 zero in front of a number if 5 digits but if 6 digits stay as it is

Posted on 2016-08-31
33 Views
I want to add 1 zero in front of 5 digit numbers but no need to add if there are 6 digit numbers in  VB.net 2010.

Example:

12258 into 012258
400488 into 400488

0
Question by:Queennie L

LVL 65

Expert Comment

The SQL Server T-SQL answer would be...
``````SELECT RIGHT('00000' + CAST(your_number as varchar(10)), 6)
``````
This requires converting the number to a character data type, as numbers do not support leading zeros.
0

LVL 69

Accepted Solution

Éric Moreau earned 500 total points
if you want to do it in VB, you can use PadLeft (https://msdn.microsoft.com/en-us/library/92h5dc07(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2):
``````dim x as string = "12258"
``````
0

Author Closing Comment

Thank you. It worked.
0

LVL 32

Expert Comment

Expanding on what Eric has stated, another way, if you are dealing with numerics specifically, is to use the ToString method with a custom format specified; e.g. -
``````Module Module1
Sub Main()
Dim values = {1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789}
For Each value In values
Console.WriteLine(value.ToString(String.Format("D{0}", values.Max().ToString().Length())))
Next
End Sub
End Module
``````
Which produces the following output -So for your specific case you could simply use:
``````Dim value = 12258
Console.WriteLine(value.ToString("D6"))
'' Output will be
'' 012258
``````

Proof of concept -
``````Module Module1
Sub Main()
Dim value = 12258
Console.WriteLine(value.ToString("D6"))
End Sub
End Module
``````
Output -
-saige-
1

## Featured Post

### Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…