Line 3 decimal places

Example 1

Dim myStr As String = "38" 

I want my result to be 38.000 ...


Example 2

 myStr = "6.4" 

I want my result to be 6.400


What is the best way to achieve this? I want to format a string variable with at least three decimal places.

+8
string format
source share
4 answers

Use FormatNumber :

 Dim myStr As String = "38" MsgBox(FormatNumber(CDbl(myStr), 3)) Dim myStr2 As String = "6.4" MsgBox(FormatNumber(CDbl(myStr2), 3)) 
+15
source share

See "Standard Number Format Strings"

 float value = 6.4f; Console.WriteLine(value.ToString("N3", CultureInfo.InvariantCulture)); // Displays 6.400 
+2
source share

So if you have

 Dim thirtyEight = "38" Dim sixPointFour = "6.4" 

Then the best way to parse them into a numeric type: Double.Parse or Int32.Parse , you should keep your data typed until you want to display it to the user.

Then, if you want to format a string with 3 decimal places , do somthing as String.Format("{0:N3}", value) .

So, if you want to quickly crack the problem,

 Dim yourString = String.Format("{0:N3}", Double.Parse("38")) 

.

+1
source share

In pseudo code

 decpoint = Value.IndexOf("."); If decpoint < 0 return String.Concat(value,".000") else return value.PadRight(3 - (value.length - decpoint),"0") 

If the string will contain the string. If it is a number, pass it as a unit.

+1
source share

All Articles