locked
vb.net convert double to integer RRS feed

  • Question

  • Hi,

    somebody know how to convert a double to int32?

    I tried over internet to find something but without good result, maybe I'm looking wrong things.

    I have a label that say me the duration of a song.

    let's say 03:01--> 3 minutes and 1 second

    how can I convert this double to int32?

    I need to convert it because need to compare with a int32 (mytrackbar .maximum = int32) is personalizated not default trackbar.

      Dim theValueToConvert As Integer = Convert.ToInt32(Player.currentMedia.duration)
    
            ' Update TrackPostion
                With Mposition
                .Minimum = 0
                .Maximum = theValueToConvert
              end with

    thanks


    Monday, July 17, 2017 12:51 PM

Answers


  • Chances are that the value "03:01" came from some actual TimeSpan duration to begin with.

    That's a good point.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, July 17, 2017 4:02 PM

All replies

  • Hi,

    somebody know how to convert a double to int32?

    I tried over internet to find something but without good result, maybe I'm looking wrong things.

    I have a label that say me the duration of a song.

    let's say 03:01--> 3 minutes and 1 second

    how can I convert this double to int32?

    I need to convert it because need to compare with a int32 (mytrackbar .maximum = int32) is personalizated not default trackbar.

    thanks

    Hi

    Here is a simple way to do it. You will need to try it out and see if the results are as you need. There is a Stop command where you can examine the results.

    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim a1 As Double = 3.000001
            Dim a2 As Double = 3.49999
            Dim a3 As Double = 3.5
            Dim a4 As Double = 3.50000001
            Dim a5 As Double = 3.9999999
    
            Dim v1 As Integer = CInt(a1)
            Dim v2 As Integer = CInt(a2)
            Dim v3 As Integer = CInt(a3)
            Dim v4 As Integer = CInt(a4)
            Dim v5 As Integer = CInt(a5)
            Stop
        End Sub


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, July 17, 2017 1:00 PM
    Monday, July 17, 2017 12:59 PM
  • TryParse won't work; TryParseExact would but here's another way:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            Const s As String = "3:01"
    
            Dim tsString() As String = s.Split(":"c)
    
            Dim totalSeconds As Integer = (CInt(tsString(0)) * 60) + CInt(tsString(1))
    
            MessageBox.Show(totalSeconds.ToString("n0") & " Seconds")
    
            Stop
    
        End Sub
    End Class


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, July 17, 2017 1:18 PM
  • TryParse won't work; TryParseExact would but here's another way:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            Const s As String = "3:01"
    
            Dim tsString() As String = s.Split(":"c)
    
            Dim totalSeconds As Integer = (CInt(tsString(0)) * 60) + CInt(tsString(1))
    
            MessageBox.Show(totalSeconds.ToString("n0") & " Seconds")
    
            Stop
    
        End Sub
    End Class


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    I just tried it but it give me an error : value to big or to small for int32

    and this is code I tried:

    'timer1_tick event
    
     Dim s As String = (Player.currentMedia.durationString)
    
            Dim tsString() As String = s.Split(":"c)
    
            Dim totalSeconds As Integer = (CInt(tsString(0)) * 60) + CInt(tsString(1))
    
            ' Display track name
            txt_trackname.Text = WPlayer.currentMedia.name
            ' Update TrackPostion
            With Mposition   'this is my trackbar
                .Minimum = 0
                .Maximum = totalSeconds
                .Value = CInt(Player.controls.currentPosition())
            End With

    Monday, July 17, 2017 1:29 PM

  • and this is code I tried:

    'timer1_tick event
    
     Dim s As String = (Player.currentMedia.durationString)
    
            Dim tsString() As String = s.Split(":"c)
    
            Dim totalSeconds As Integer = (CInt(tsString(0)) * 60) + CInt(tsString(1))
    
            ' Display track name
            txt_trackname.Text = WPlayer.currentMedia.name
            ' Update TrackPostion
            With Mposition   'this is my trackbar
                .Minimum = 0
                .Maximum = totalSeconds
                .Value = CInt(Player.controls.currentPosition())
            End With

    That should work - did it not?

    If not then put a breakpoint in and see what's going on. I don't know with .currentPosition returns (so I don't know if total seconds is correct).

    Do you know? If not then do some research first...


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, July 17, 2017 1:32 PM
  • Paolo,

    If this is your player, then .currentPosition is in seconds:

    https://msdn.microsoft.com/en-us/library/windows/desktop/dd564740(v=vs.85).aspx

    It's a Double but an integer will work fine (it's a widening conversion).


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, July 17, 2017 1:41 PM
  • In my perception you cannot do that. An Integer simply has nothing behind the decimal point. 

    However, you can of course first multiply it by 100 and then do that. 

    But I don't see the sense for converting a double to an integer (a decimal I would understand), but I assume you do. 

            Dim strArra = "03:01".Split(":"c)
            Dim myInter = 100 * (CInt(strArra(0)) + CInt(strArra(1)))



    Success
    Cor

    Monday, July 17, 2017 2:44 PM
  • No Frank I'm sorry but you are confusing my problem.

     the value of my player is ok.

    .value works good for me

    the problem is on .maximum value.

    my trackbar accept maximum(.maximum) value as  integer.

    I need to tell what is the maximum value of my trackbar.

    and the maximum value is the duration of song.

    but duration of song is a double value like I told "03:01"

    Monday, July 17, 2017 2:49 PM
  • No Frank I'm sorry but you are confusing my problem.

     the value of my player is ok.

    .value works good for me

    the problem is on .maximum value.

    my trackbar accept maximum(.maximum) value as  integer.

    I need to tell what is the maximum value of my trackbar.

    and the maximum value is the duration of song.

    but duration of song is a double value like I told "03:01"

    I'm lost now about what the question is but I'll add this: You potentially lose resolution when you cast a double down to an integer. For example:

    Dim d As Double = 5.2
    Dim i As Integer = CInt(d)
    
    Stop

    The value of "i" is 5. If you're using that as the .Maximum of a control then it will throw an exception if the actual value is greater than 5.

    You might cheat it some and add one second to the .Maximum property.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, July 17, 2017 2:54 PM
  • You don't actually need to think in terms of converting one numeric type to another... the problem is a unit conversion, not a type conversion (and you're thinking of the wrong type anyway... there is no Double value involved, or shouldn't be).

    The trackbar needs to be in a particular unit, or scale.  In this case, seconds seems to be the correct unit.

    The track duration is displayed in a time format, using minutes and seconds - this is not a Double value but rather a TimeSpan.  This time needs to be interpreted as whole seconds in order to convert between the displayed track time and the maximum duration needed for the trackbar.

    So ultimately you want to store "03:01" as a TimeSpan instance and then use the TotalSeconds property to get the maximum duration of the trackbar.  Note that you could use milliseconds or some other interval if desired.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, July 17, 2017 3:16 PM
  • You don't actually need to think in terms of converting one numeric type to another... the problem is a unit conversion, not a type conversion (and you're thinking of the wrong type anyway... there is no Double value involved, or shouldn't be).

    The trackbar needs to be in a particular unit, or scale.  In this case, seconds seems to be the correct unit.

    The track duration is displayed in a time format, using minutes and seconds - this is not a Double value but rather a TimeSpan.  This time needs to be interpreted as whole seconds in order to convert between the displayed track time and the maximum duration needed for the trackbar.

    So ultimately you want to store "03:01" as a TimeSpan instance and then use the TotalSeconds property to get the maximum duration of the trackbar.  Note that you could use milliseconds or some other interval if desired.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    My original reply was to use a TimeSpan but TimeSpan.TryParse interprets the "3" as the hours and "01" as minutes.

    Rather than going with TimeSpan.TryParseExact, I figured it was less formidable code to come up with total seconds via direct conversion of the two elements of the string representation.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, July 17, 2017 3:20 PM
  • ... not a type conversion (and you're thinking of the wrong type anyway... there is no Double value involved, or shouldn't be).

    Yes there is.

    The .currentPosition property is a Double and that's my concern: If the actual position (in seconds as a double) is greater than the narrowing of the Double to an Integer, an exception will be thrown.

    Short of cheating it with a higher value, I don't see a solution in this case.

    No it won't ... the fact that his control returns a double but he's casting that down to an integer, then .Maximum will never be exceeded by the .Value because .Value used the same thing!


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Monday, July 17, 2017 3:25 PM
  • You don't actually need to think in terms of converting one numeric type to another... the problem is a unit conversion, not a type conversion (and you're thinking of the wrong type anyway... there is no Double value involved, or shouldn't be).

    The trackbar needs to be in a particular unit, or scale.  In this case, seconds seems to be the correct unit.

    The track duration is displayed in a time format, using minutes and seconds - this is not a Double value but rather a TimeSpan.  This time needs to be interpreted as whole seconds in order to convert between the displayed track time and the maximum duration needed for the trackbar.

    So ultimately you want to store "03:01" as a TimeSpan instance and then use the TotalSeconds property to get the maximum duration of the trackbar.  Note that you could use milliseconds or some other interval if desired.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    My original reply was to use a TimeSpan but TimeSpan.TryParse interprets the "3" as the hours and "01" as minutes.

    Rather than going with TimeSpan.TryParseExact, I figured it was less formidable code to come up with total seconds via direct conversion of the two elements of the string representation.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    I would still go the TimeSpan route and either use a parsing mask or pad the input string with the appropriate additional characters.

    Chances are that the value "03:01" came from some actual TimeSpan duration to begin with.  My suggestion would be to back to up to the point where that duration value is initially acquired and see if it isn't available as a TimeSpan or total-seconds to begin with.

    Either way, this is definitely an issue of unit conversion rather than one of type conversion.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, July 17, 2017 3:44 PM
  • ... not a type conversion (and you're thinking of the wrong type anyway... there is no Double value involved, or shouldn't be).

    Yes there is.

    The .currentPosition property is a Double and that's my concern: If the actual position (in seconds as a double) is greater than the narrowing of the Double to an Integer, an exception will be thrown.

    Short of cheating it with a higher value, I don't see a solution in this case.

    No it won't ... the fact that his control returns a double but he's casting that down to an integer, then .Maximum will never be exceeded by the .Value because .Value used the same thing!


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    If that is the case, and the CurrentPosition (and/or the MaxDuration) is a double value representing a unit of seconds, then the trackbar should use units of milliseconds, or tenths of a second, or some appropriately precise value.  Using TimeSpan.FromSeconds would then be the simplest way to perform the unit conversion and maintain all precision.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, July 17, 2017 4:00 PM

  • Chances are that the value "03:01" came from some actual TimeSpan duration to begin with.

    That's a good point.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, July 17, 2017 4:02 PM
  • Thank to all of you to helped me solving the problem.

    I resolved it changing the personalized trackbar (.dll download from an open source) and used this one

    --> Trackbar

    It work good and have a lot of function, a wonderful work.

    integer to int32 or string to integer and so on make me mad.

    my code now is this:

        Private Sub UpdatePlayerTimer(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
           With trackposition
                    .MinValue = 0
                   
                    .MaxValue = CInt(Player.currentMedia.duration)
                    .Value = CInt(Player.controls.currentPosition())
                End With
        End Sub
    
     Private Sub track_Scroll(sender As Object, e As ScrollEventArgs) Handles track.Scroll
         
            Player.controls.pause()
            Player.controls.currentPosition = track.Value
            Player.controls.play()
            Application.DoEvents()
        End Sub

    Thanks to all, solved problem.

    Wednesday, July 19, 2017 12:47 PM
  • Hi paoloDeBeneditis,

    Gold to hear this issue has been solved by yourself. Thanks for your sharing, please mark your last post as answer, it is beneficial to other community members who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 1, 2017 4:29 AM