Results 1 to 16 of 16

Thread: Speed and Degree conversion string

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2023
    Posts
    5

    Speed and Degree conversion string

    Hi all,
    i have a problem converting two strings taken from 2 columns of one datagridview.

    One column is "speed" with valor "0.277778" but also "116.786023"
    One column is "latitude" with valor "38.284572"

    So I need to have speed with only 2 decimal after the point and I need to transform valor latitude from "38.284572" to "3828.4572"

    For latitude I use this code:
    Code:
    Dim valor As String = CStr(DataGridView1.Rows(10).Cells("latitude").Value)
    Dim sde As String = valor.Replace(".", "")
    Dim lat As String = sde.Insert(4, ".")
    I don't know if there is a simpler function

    For speed I tryed with:

    Code:
    Format(CDec(DataGridView1.Rows(10).Cells("speed").Value), "0.00")
    But I get "277778.00".. but I need "0.27"... or "116.78" for second valor .. How I can resolve?

  2. #2
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Speed and Degree conversion string

    Why do you want to convert you values into string then modify them?
    play directly with the values then convert to string if you need to show them.

    What are the type of DataGridView1.Rows(10).Cells("latitude") and DataGridView1.Rows(10).Cells("speed") ?

    Code:
    Dim speed as "correct type"
    Speed = math.round(DataGridView1.Rows(10).Cells("speed").Value, 2,MidpointRounding.ToEven)
    
    Dim latitude as "correct type"
    latitude =DataGridView1.Rows(10).Cells("latitude").Value * 100
    or directly

    Code:
    Dim speed  = math.round(DataGridView1.Rows(10).Cells("speed").Value, 2,MidpointRounding.ToEven)
    
    Dim latitude = DataGridView1.Rows(10).Cells("latitude").Value * 100
    Last edited by Delaney; Feb 7th, 2023 at 05:12 AM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2023
    Posts
    5

    Re: Speed and Degree conversion string

    is not possible.. for latitude:
    Code:
    Dim latitude = DataGridView1.Rows(10).Cells("latitude").Value * 100
    result is "3828457200" from an initial value of "38.284572"
    I thought about it too, but since they are not decimal numbers, the points are in different positions, so I had to use the removal of the point from one position and moved to another position.


    For speed I get error
    cell of datagridview are type string.. and I get this error:

    System.Reflection.AmbiguousMatchException: 'Risoluzione dell'overload non riuscita. Nessun elemento 'Round' pubblico può essere chiamato senza una conversione verso un tipo di dati più piccolo:
    'Public Shared Function Round(value As Double, digits As Integer, mode As System.MidpointRounding) As Double':
    if I modify in:
    Code:
    Dim speed = Math.Round(cdec(DataGridView1.Rows(10).Cells("speed").Value), 2, MidpointRounding.ToEven)
    result is "277778"
    from an initial value of "0.277778" but I need to get all number before point, and only 2 number after point

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Speed and Degree conversion string

    That doesn't make sense, to me. I'd want to do a bit more study of the problem. For example, if the value in the Latitude cell is really "38.284572", then there is no way that CDec(DataGridView1.Rows(10).Cells("latitude").Value) * 100 can be anything other than 3838.4572, and yet it looks like you are getting something else. Therefore, I think you ought to take a closer look at just this:

    CDec(DataGridView1.Rows(10).Cells("latitude").Value)

    what does that return?
    My usual boring signature: Nothing

  5. #5
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Speed and Degree conversion string

    Quote Originally Posted by thesaint76 View Post
    is not possible.. for latitude:
    Code:
    Dim latitude = DataGridView1.Rows(10).Cells("latitude").Value * 100
    result is "3828457200" from an initial value of "38.284572"
    I thought about it too, but since they are not decimal numbers, the points are in different positions, so I had to use the removal of the point from one position and moved to another position.
    are you sure that DataGridView1.Rows(10).Cells("latitude").Value=38.28... or it is what it is show in the datagridview ?

    just do a msgbox(DataGridView1.Rows(10).Cells("latitude").Value.tostring) to check


    For speed I get error
    cell of datagridview are type string.. and I get this error:



    if I modify in:
    Code:
    Dim speed = Math.Round(cdec(DataGridView1.Rows(10).Cells("speed").Value), 2, MidpointRounding.ToEven)
    result is "277778"
    from an initial value of "0.277778" but I need to get all number before point, and only 2 number after point
    try
    Code:
    dim converted_value as double
    double.tryparse(DataGridView1.Rows(10).Cells("speed").Value, converted_value)
    Dim speed = Math.Round(converted_value, 2, MidpointRounding.ToEven))

    Comment: I thing the CDec may do strange conversion from a string. I rarely use decimal, i prefer double who are just enough for most of the case

    edit : I just tested that
    Code:
    Label1.Text = Math.Round(CDec("0.277778"), 2, MidpointRounding.ToEven).ToString
    and I got what I should expect : 0.28

    so check also the real contents of DataGridView1.Rows(10).Cells("speed").Value
    Last edited by Delaney; Feb 7th, 2023 at 07:53 AM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  6. #6
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Speed and Degree conversion string

    I posit that the confounding mathematical results are happening because the decimal point is being interpreted as a thousands separator.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Speed and Degree conversion string

    That would explain it, if that's how it works.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2023
    Posts
    5

    Re: Speed and Degree conversion string

    Quote Originally Posted by Delaney View Post
    are you sure that DataGridView1.Rows(10).Cells("latitude").Value=38.28... or it is what it is show in the datagridview ?

    just do a msgbox(DataGridView1.Rows(10).Cells("latitude").Value.tostring) to check
    msgbox(DataGridView1.Rows(10).Cells("latitude").Value.tostring) I have "38.284572"


    also I make:

    Code:
    Dim speed = Math.Round(CDec("0.277778"), 2, MidpointRounding.ToEven).ToString
    MsgBox(speed)
    result ever "277778" maybe regional issue?


    My datagridview get valor from file csv (comma separated). I import it

    Code:
    dt.Columns.Add("speed", GetType(String))
    ....
    fName = OpenFileDialog1.FileName
    Dim TextLine As String = ""
    Dim SplitLine() As String
    
    If System.IO.File.Exists(fName) = True Then
         Dim objReader As New System.IO.StreamReader(fName, Encoding.ASCII)
         Dim index As Integer = 0
    
         Do While objReader.Peek() <> -1
               If index > 0 Then
                            TextLine = objReader.ReadLine()
                            SplitLine = Split(TextLine, ",")
                            dt.Rows.Add(SplitLine)
                    Else
                            TextLine = objReader.ReadLine()
                End If
          index = index + 1
          Loop
          DataGridView1.DataSource = dt
       Else
          MsgBox("File Does Not Exist")
    End If

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2023
    Posts
    5

    Re: Speed and Degree conversion string

    wait... With this I get right result:

    Code:
    Dim speed = Math.Round(CDec(0.277778), 2, MidpointRounding.ToEven).ToString
    MsgBox(speed)
    result "0.28"

    So without "" ? and how I can resolve

  10. #10
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Speed and Degree conversion string

    since this is in .NET ... why not use Decimal.TryParse instead of CDEC?


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Speed and Degree conversion string

    CDec is better so long as you KNOW that the string can always be converted successfully. That seems likely, in this case. Of course, if any cells are empty, then CDec will fail on those.

    This is a culture issue, from the looks of it. CDec on a string is going to use the cultural settings. CDec on a numeric literal is going to see a . as a decimal point in any culture, I believe.

    You probably need to use Decimal.Parse with one of the overloads using culture information, but I've never done that:

    https://learn.microsoft.com/en-us/do...arsing-numeric
    My usual boring signature: Nothing

  12. #12
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Speed and Degree conversion string

    The regional issue would explain. check you regional setting to see if you have a . for thousand separation.

    That's not something I didn't think about as it is the first thing I change on any new computer I have; I replace everywhere comma by dot for the decimal separator and remove any separator for thousand.

    Edit: after checking , the thousand separator in Italia is a dot so it should be that
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  13. #13
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Speed and Degree conversion string

    You may change the culture for your application only. have a look here :

    https://learn.microsoft.com/fr-fr/do...e?view=net-7.0
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  14. #14
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Speed and Degree conversion string

    Quote Originally Posted by Shaggy Hiker View Post
    CDec is better so long as you KNOW that the string can always be converted successfully. That seems likely, in this case. Of course, if any cells are empty, then CDec will fail on those.

    This is a culture issue, from the looks of it. CDec on a string is going to use the cultural settings. CDec on a numeric literal is going to see a . as a decimal point in any culture, I believe.

    You probably need to use Decimal.Parse with one of the overloads using culture information, but I've never done that:

    https://learn.microsoft.com/en-us/do...arsing-numeric
    Personally I tend towards either Decimal.Parse / TryParse, partly because I can use the same in both VB and C# but I also find the syntax a bit more expressive. Under the hood CDec does a bit more work and is slightly slower too; admittedly the difference is measured in nanoseconds and not likely to make a difference unless performed inside of some very large loops....

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Speed and Degree conversion string

    I'm not going to go seek it out, but I was surprised at one point to find that MS advocated the use of CDec, CInt, etc. over the .Parse methods. I don't remember why.
    My usual boring signature: Nothing

  16. #16

    Thread Starter
    New Member
    Join Date
    Feb 2023
    Posts
    5

    Re: Speed and Degree conversion string

    okk RESOLVED

    Code:
    CultureInfo.CurrentCulture = New CultureInfo("en-US")
    Dim speed As String = Math.Round(CDec(DataGridView1.Rows(10).Cells("speed").Value), 2, MidpointRounding.ToEven).ToString
    msgbox(speed) and now we have 0.28

    very Thankns to all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width