-5

I need to convert the str_Delay value to ms(millisecond). I tried the following am getting result as 0. Please help in this.

  Dim str_Delay As Integer = "0.01"
  Dim str_Delay1 As Integer
  str_Delay1 =  
  TimeSpan.FromSeconds(str_Delay).
                    TotalMilliseconds)
  MsgBox(str_Delay1)
user692942
  • 16,398
  • 7
  • 76
  • 175
  • 1
    isn't that VB.Net ? – Slai Feb 07 '20 at 09:08
  • 2
    If you used [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) it would tell you that you should not be trying to directly assign the string`"0.01"` to an integer. An integer is a whole number, so it can't represent 0.01 anyway. – Andrew Morton Feb 07 '20 at 11:16
  • 2
    And drop the (inaccurate) Hungarian notation. `str_Delay` is an Integer and is misleading. Just use variables named `delay` and `delay1` instead. – HardCode Feb 07 '20 at 20:19

2 Answers2

0

There are 1000 milliseconds in a second, so just convert the string to number, and multiply by 1000 :

str_Delay1 = Val(str_Delay) * 1000
Slai
  • 22,144
  • 5
  • 45
  • 53
0

Change your data types to Double. You cannot represent 0.01 as an Integer. If you debug your code and look at your values, it's not that the conversion to milliseconds is resulting in 0, str_Delay is 0 to begin with. So of course the final result is zero as well.

Dim str_Delay As Double = 0.01
Dim str_Delay1 As Double = TimeSpan.FromSeconds(str_Delay).TotalMilliseconds

MsgBox(str_Delay1)
Anu6is
  • 2,137
  • 2
  • 7
  • 17