Dim myStr As String = "38"
I want my result to be 38.000
...
myStr = "6.4"
I want my result to be 6.400
What is the best method to achieve this? I want to format a string variable with atleast three decimal places.
What does Rounding Off to Three Decimal Places Mean? Rounding to Three Decimal Places is the process of rounding to thousandths place i.e. 3nd place to the right of decimal point. For Example 5.5380 rounded to three decimal places is 5.538.
string decimal-string(number, number) The first number parameter is the number to be rounded. The second parameter gives the number of digits of precision past the decimal point to use.
String strDouble = String. format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.
Use FormatNumber:
Dim myStr As String = "38"
MsgBox(FormatNumber(CDbl(myStr), 3))
Dim myStr2 As String = "6.4"
MsgBox(FormatNumber(CDbl(myStr2), 3))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With