# Thread: Speed and Degree conversion string

1. ## 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. ## 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

3. ## 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. ## 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?

5. ## Re: Speed and Degree conversion string

Originally Posted by thesaint76
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

6. ## 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. ## Re: Speed and Degree conversion string

That would explain it, if that's how it works.

8. ## Re: Speed and Degree conversion string

Originally Posted by Delaney
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:
....
fName = OpenFileDialog1.FileName
Dim TextLine As String = ""
Dim SplitLine() As String

If System.IO.File.Exists(fName) = True Then
Dim index As Integer = 0

If index > 0 Then
SplitLine = Split(TextLine, ",")
Else
End If
index = index + 1
Loop
DataGridView1.DataSource = dt
Else
MsgBox("File Does Not Exist")
End If

9. ## 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. ## Re: Speed and Degree conversion string

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

-tg

11. ## 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

12. ## 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

13. ## 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

14. ## Re: Speed and Degree conversion string

Originally Posted by Shaggy Hiker
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. ## 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.

16. ## 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
•