Format String to 2 Decimal Places VB.Net
This blog post demonstrates how to format float numbers to string format in VB.net. You can use static method String.Format or instance methods double.ToString and float.ToString. Find the vb.net code snippet below :-
string format double |
Console App
Module Module1
Sub Main()
Dim list As ArrayList = New ArrayList()
list.Add("5")
list.Add("60")
list.Add("25.5")
list.Add("85.56")
' Get array of objects.
Dim array() As Object = list.ToArray()
For Each element In array
'Cast object to Decimal
Dim value As Decimal = element
' 1st way
Console.WriteLine(Format(value, "0.00"))
Console.WriteLine()
' 2nd way
Console.WriteLine(CStr(value.ToString("#,##0.00")))
Console.WriteLine()
' 3rd way
Console.WriteLine((value.ToString("N2")))
Console.WriteLine()
' 4th way
Console.WriteLine(String.Format("{0:f2}", CType(value, Decimal)))
Next
Console.ReadKey()
End Sub
End Module
Format String to 2 Decimal Places VB.Net
Reviewed by Ravi Kumar
on
10:16 PM
Rating:
No comments: