Formating Numbers in Visual Basic
In Visual Basic you can easily format numbers the way you want it to be displayed by using the format() function. Format(expression, format) applying the following symbols ('#') (',') ('0')
'#' symbol represents that a number in this position will be displayed if the number is available and is greater than zero. '0' on the other hand represents that a number in this particular position will be displayed even if it is not available. Lastly ',' is used as a separator for thousands or millions and so on. See the example below to have a better understanding on how to apply this format function in your programming code.
'---Displaying figures in textbox1.text
Dim MyNumber1, MyNumber2, MyNumber3 As Double
MyNumber1 = 4985746.3
MyNumber2 = 0.48
MyNumber3 = 3142
textbox1.text = Format(MyNumber1, "#,###,##0.00") 'displayed as : 4,985,746.30
textbox1.text = Format(MyNumber1, "######0.00") 'displayed as : 4985746.30
textbox1.text = Format(MyNumber1, "######0.0000") 'displayed as : 4985746.3000
textbox1.text = Format(MyNumber2, "#,###.00") 'displayed as : 0.48
textbox1.text = Format(MyNumber2, "####.00") 'displayed as : 0.48
textbox1.text = Format(MyNumber2, "#,##0.00") 'displayed as : 0.48
textbox1.text = Format(MyNumber2, "###0.0") 'displayed as : 0.4
textbox3.text = Format(MyNumber3, "###0.0") 'displayed as 3142.0
textbox3.text = Format(MyNumber3, "###0.0000") 'displayed as 3142.0000
textbox3.text = Format(MyNumber3, "####.00") 'displayed as 3142.00
textbox3.text = Format(MyNumber3, "####.##") 'displayed as 3142
textbox3.text = Format(MyNumber3, "#,###.##") 'displayed as 3,142
That explains how number formatting works in Visual Basic.
Subscribe to:
Post Comments (Atom)

0 comments
Post a Comment