Results 1 to 15 of 15

Thread: [RESOLVED] Change a value separation from comma to a period.

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    8

    Resolved [RESOLVED] Change a value separation from comma to a period.

    Hello,
    I´m trying to convert a string to a value but when I convert it, the string that have a period separating changes to a comma but I need the number with the period separating the units from decimal part number...
    For example, "41.235556" when I convert this string to a value it returns with "41,235556" but instead of a comma I need a period like the string!

    Code:
    Dim destlongitude As String
            
            destlongitude =("41.235556")
    
            MsgBox(Val(destlatitude))
    
        'RESULT of messagebox: 41,235556 but I need 41.235556
    Thanks in advance

  2. #2
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: Change a value separation from comma to a period.

    First of all the message box is showing the latitude. Second, why not try to use a decimal instead of a string?

    Code:
            Dim destlongitude As Decimal
    
            destlongitude = 41.235556
    
            MsgBox(destlongitude)

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    8

    Re: Change a value separation from comma to a period.

    Quote Originally Posted by NinjaNic View Post
    First of all the message box is showing the latitude. Second, why not try to use a decimal instead of a string?

    Code:
            Dim destlongitude As Decimal
    
            destlongitude = 41.235556
    
            MsgBox(destlongitude)
    Thanks for the quick reply dear NinjaNic
    What I put over there it was just an example but I can show you all the code, because what happens is that the program goes to a txt file search for a line of that file and then on that line he searchs for latitude and longitude of that point! So when latitude and longitude comes from the txt file it comes as a string and then I nead to use that value on a different thing that don´t let me put a string...

    code:
    Code:
     Using reader As New System.IO.StreamReader("airports.txt")
                                While Not reader.EndOfStream
                                    airportline = reader.ReadLine()
                                    If airportline.Contains("LPPR") Then
                                        Exit While
                                    End If
                                End While
                            End Using
                            arptln = Split(airportline, ",")
                            destlatitude = arptln(3)
                            destlongitude = arptln(4)
    
                            Dim lat As FsLatitude = New FsLatitude(Val(destlatitude))
                            Dim lon As FsLongitude = New FsLongitude(Val(destlongitude))
                            arpt = New FsLatLonPoint(lat, lon)
    Once again, thank you

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

    Re: Change a value separation from comma to a period.

    I totally agree. You don't have your location mentioned in the profile I see, but it appears that you have a regional setting that uses commas rather than points for decimal designators. That's fine for displaying strings in whatever regional localization makes sense, but a number should be held as a numeric type, rather than as a string representation of a number.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    8

    Re: Change a value separation from comma to a period.

    Quote Originally Posted by Shaggy Hiker View Post
    I totally agree. You don't have your location mentioned in the profile I see, but it appears that you have a regional setting that uses commas rather than points for decimal designators. That's fine for displaying strings in whatever regional localization makes sense, but a number should be held as a numeric type, rather than as a string representation of a number.
    thanks Shaggy Hiker, I´m from Portugal. Do you know how do I change decimal designators to points?

  6. #6
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: Change a value separation from comma to a period.

    You could try something like this:

    Code:
            Dim TestString As String = "12345.6789"
            Try
                Dim TestDecimal As Decimal = CDec(TestString)
                MsgBox(CStr(TestDecimal))
            Catch ex As Exception
                MsgBox("Not a valid number.")
            End Try
    Do decimal values show you the comma or just the string shows you the comma?

    Edit: Never mind, I hadn't seen your post yet when I added this.

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    8

    Re: Change a value separation from comma to a period.

    Quote Originally Posted by NinjaNic View Post
    You could try something like this:

    Code:
            Dim TestString As String = "12345.6789"
            Try
                Dim TestDecimal As Decimal = CDec(TestString)
                MsgBox(CStr(TestDecimal))
            Catch ex As Exception
                MsgBox("Not a valid number.")
            End Try
    Do decimal values show you the comma or just the string shows you the comma?

    Edit: Never mind, I hadn't seen your post yet when I added this.
    When I use Your proposal code the result of messagebox is "123456789" without any kind of separation From what I search on google I think the problem is the regional localization but I don´t know how to change it....

  8. #8
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Change a value separation from comma to a period.

    If you were going to store floating point numbers as strings in a flat text file, then you would do so using the period as the decimal separator, and not use any thousands separators. Basically, you would convert the numbers to text using the conventions of the Invariant Culture. Using the Invariant Culture to convert your data to and from text will give consistent results, regardless of the current culture that you (or those that you are sharing the data with) have set.

    But, I'm confused:
    Quote Originally Posted by bruno97 View Post
    So when latitude and longitude comes from the txt file it comes as a string and then I nead to use that value on a different thing that don´t let me put a string...
    So why are you converting it back into a string (which is what the MsgBox(Val(number as string)) is doing implicitly)?



    Anyway,if you need to convert a number to a string, with a period as decimal separator, irrespective of your current culture, you could use something similar to:
    Code:
    Dim numberAsString As String
    
    numberAsString = Convert.ToString(Val("1.23"), Globalization.CultureInfo.InvariantCulture)

    EDIT: Note that the Val function will only work with string numbers that use the period as the decimal separator. It will fail with, for example:Val("1,23"), which is using a comma as the decimal separator.
    Last edited by Inferrd; Jan 10th, 2015 at 01:10 PM. Reason: added a bit of explanation

  9. #9

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    8

    Re: Change a value separation from comma to a period.

    Sorry... I´m quite noob on forums and I see that I didn´t explain my self very well...

    So why are you converting it back into a string (which is what the MsgBox(Val(number as string)) is doing implicitly)?
    You are right doesn´t make sense to do it but my main objective is not what exposed in first post... My objective is that I have a database in txt where I have stored many different points that have the coordinates of that point. So I have to put that coordinates in txt file to visual basic but when they come from txt file those coordinates are strings and I need them as decimal.

    Code:
    Using reader As New System.IO.StreamReader("airports.txt")
                                While Not reader.EndOfStream
                                    airportline = reader.ReadLine()
                                    If airportline.Contains("LPPR") Then
                                        Exit While
                                    End If
                                End While
                            End Using
                            arptln = Split(airportline, ",")
                            destlatitude = arptln(3)
                            destlongitude = arptln(4)
    
                            Dim lat As FsLatitude = New FsLatitude(Val(destlatitude))
                            Dim lon As FsLongitude = New FsLongitude(Val(destlongitude))
                            arpt = New FsLatLonPoint(lat, lon)
    So the variables "destlatitude" and "destlongitude" are strings and when I use them they must be a value like decimal but with period separation!
    Hope that I explain my self better... thank you

  10. #10
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Change a value separation from comma to a period.

    OK

    So (destlatitude) and (destlongitude) are Strings.

    The Val function converts the strings to Numeric Types (Doubles).
    You don't need to worry about decimal separators when dealing with Numeric Types.

    (Numeric Types are stored internally in a different manner from the way strings are stored. A Numeric Type doesn't have a concept of periods or commas or decimal/thousands separators. They're just collections of Bits in a specific order).

    You would then use the Numeric Types to perform any calculations.

    Once you have your result, if you want to store it back to your txt file, or display it to the user in a TextBox or MessageBox or whatever, you need to convert the result (Numeric Type) to a String. It's only in this conversion to a String that you need to worry about decimal separators being commas or periods.

    Hopefully, the following commented code will help a little in explaining this:
    VB.NET Code:
    1. Dim destlongitude1 As String ' Text
    2. Dim destlongitude2 As String ' Text
    3.  
    4. Dim long1AsDouble As Double ' Numeric
    5. Dim long2AsDouble As Double ' Numeric
    6.  
    7. Dim finalDestination As Double ' Numeric
    8.  
    9. destlongitude1 = ("41.235556") ' assign text value
    10. destlongitude2 = ("21.563412") ' assign text value
    11.  
    12. long1AsDouble = Val(destlongitude1) ' convert text to Numeric Type
    13. long2AsDouble = Val(destlongitude2) ' convert text to Numeric Type
    14.  
    15.  
    16.  
    17. finalDestination = long1AsDouble + long2AsDouble ' work with NUMERIC Type values
    18.  
    19.  
    20. 'need a string to display to user
    21. ' (or store in a txt file)
    22. ' and want a period for a decimal separator, so use Invariant Culture
    23. Dim textToDisplay As String
    24. textToDisplay = Convert.ToString(finalDestination, Globalization.CultureInfo.InvariantCulture)
    25.  
    26. ' show it to the user as a STRING, with a decimal point
    27. MsgBox(textToDisplay)

    Sorry for the dreadful variable names, but I'm just trying to illustrate a point

  11. #11

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    8

    Re: Change a value separation from comma to a period.

    thank you very much amazing work that you did but unfortunately doesn't work for the code that I´m using....
    I don´t want to show to the user the result, instead I want to use what is inside the string destlongitude1 inside this:
    Code:
    Dim lat As FsLatitude = New FsLatitude(41.235556)
    Instead of "41.235556" it must be a variable because those number changes from point to point.... If I put the variable destlongitude1 he says that is not possible because it can´t be a string! so I need to have a numeric value to put there! Please tell me If you did understand otherwise I will try to explain even more otherwise you will be with a lot of work for a solution that don´t feet to me thanks so much for your work

  12. #12
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Change a value separation from comma to a period.

    Sorry, but I'm not really understanding your problem.

    I understand that you are retrieving lines from a text file, splitting them into fields, and assigning the value of the longitude and latitude fields to String variables. Your code is along the lines of:
    Code:
    Dim destlatitude As String
    Dim destlongitude As String
    
    arptln = Split(airportline, ",")
    destlatitude = arptln(3)
    destlongitude = arptln(4)
    
    Dim lat As FsLatitude = New FsLatitude(Val(destlatitude))
    So you have a Class named FsLatitude and its instantiator expects a Numeric type of some sort as you say
    Quote Originally Posted by bruno97 View Post
    If I put the variable destlongitude1 he says that is not possible because it can´t be a string! so I need to have a numeric value to put there!

    can you tell me:
    What type of number does it want?
    Can you show the method declaration for that instantiator? (i.e the line in the Class that starts like Public Sub New(....something here?...
    Also, what exactly is the error message you get?

    I don't understand mainly because this line:
    Code:
    Dim lat As FsLatitude = New FsLatitude(Val(destlatitude))
    is passing Val(destlatitude) to the instantiator, and that converts the string destlatitude to an actual number (a double), so you are providing a numeric value and not a string.

    You could also do it in several steps if you wanted, like:
    Code:
    Dim destlatitude As String
    Dim destlongitude As String
    
    arptln = Split(airportline, ",")
    destlatitude = arptln(3)
    destlongitude = arptln(4)
    
    Dim destlatitudeValue As Double = Val(destlatitude)
    Dim destlongitudeValue As Double = Val(destlongitude)
    
    Dim lat As FsLatitude = New FsLatitude(destlatitudeValue)
    Dim lon As FsLongitude = New FsLongitude(destlongitudeValue)
    and then there is no doubt you are passing a numeric type.

  13. #13

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    8

    Re: Change a value separation from comma to a period.

    So here is the code with almost everything that is needed for the purpose! I had a dll as reference! that dll makes visual basic capable of read certain values given by a program named FSUIPC which connects with the Flight Simulator!
    The main objective is the user gives an airport and the program calculate the distance between that airport and the currect location of the user on the flight simulator.
    Code:
     Imports FSUIPC
    Imports System.Drawing.Drawing2D
    Public Class Form1
    
    Dim playerLatitude As Offset(Of Long) = New Offset(Of Long)(&H560) ' Offset for Lat/Lon features
    Dim playerLongitude As Offset(Of Long) = New Offset(Of Long)(&H568) ' Offset for Lat/Lon features
    Dim EGLL As FsLatLonPoint ' Holds the position of London Heathrow (EGLL)
    
    Public Sub New()
            InitializeComponent()
            ' Setup the example data for London Heathrow
            ' 1. The position
            '    This shows an FsLongitude and FsLatitude class made from the Degrees/Minutes/Seconds constructor.
            '    The Timer1_Tick() method shows a different contructor (using the RAW FSUIPC values).
            Dim lat As FsLatitude = New FsLatitude(51, 28, 39D)
            Dim lon As FsLongitude = New FsLongitude(0, -27, -41D)
            EGLL = New FsLatLonPoint(lat, lon)
    end sub
    
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
     ' Latitude and Longitude 
                ' Shows using the FsLongitude and FsLatitude classes to easily work with Lat/Lon
                ' Create new instances of FsLongitude and FsLatitude using the raw 8-Byte data from the FSUIPC Offsets
                Dim lon As FsLongitude = New FsLongitude(playerLongitude.Value)
                Dim lat As FsLatitude = New FsLatitude(playerLatitude.Value)
    
    ' Using fsLonLatPoint to calculate distance and bearing between two points
                ' First get the point for the current plane position
                Dim currentPosition As FsLatLonPoint = New FsLatLonPoint(lat, lon)
                ' Get the distance between here and EGLL
                Dim distance As Double = 0
    distance = currentPosition.DistanceFromInNauticalMiles(EGLL)
    Me.txtDistance.Text = distance
    end sub
    So where is the problem? In this code that you see above we are calculating the distance between present position and london heatrow airport.
    So I changed the code in order to the user be able to chose the airport.
    For that my application goes to a database(txt file) locate the request airport that user chose and then gets the coordinates of that airport.
    that coordinates must go here:
    Code:
    Dim lat As FsLatitude = New FsLatitude(51.470226) 'instead of this:51.470226 it must be this: val(destlatitude)
            Dim lon As FsLongitude = New FsLongitude(-0.454339) 'instead of this:-0.454339 it must be this: val(destlongitude)
            EGLL = New FsLatLonPoint(lat, lon)
    ' instead of EGLL must be the variable "arpt"

    So here is the problem because I get the coordinates as string and I must convert it to a numeric and I do it by putting val(destlatitude)! when I do this, If I have this on "destlatitude" string "51.381451" he change to this "51,381451" when I convert! this value with a comma can´t be read and I know this because when we use a comma instead of point he gives me rong distances! there is no erros shown! the unique error that appears is if you try to put destlatitude without converting it to a numeric value!

  14. #14

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    8

    Re: Change a value separation from comma to a period.

    Quote Originally Posted by Inferrd View Post
    You could also do it in several steps if you wanted, like:
    Code:
    Dim destlatitude As String
    Dim destlongitude As String
    
    arptln = Split(airportline, ",")
    destlatitude = arptln(3)
    destlongitude = arptln(4)
    
    Dim destlatitudeValue As Double = Val(destlatitude)
    Dim destlongitudeValue As Double = Val(destlongitude)
    
    Dim lat As FsLatitude = New FsLatitude(destlatitudeValue)
    Dim lon As FsLongitude = New FsLongitude(destlongitudeValue)
    and then there is no doubt you are passing a numeric type.
    Ok you solved my problem thaaank you sooo much! I just don´t understand how you did it just by doing this:
    Code:
    Dim destlatitudeValue As Double = Val(destlatitude)
    Dim destlongitudeValue As Double = Val(destlongitude)
    but there is no problem! the important is that it is functional and I will understand it by my self because I don´t want to take your time :P Thank you so much, wish you nice projects
    Last edited by bruno97; Jan 10th, 2015 at 04:45 PM.

  15. #15
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: [RESOLVED] Change a value separation from comma to a period.

    Glad to hear it's working. I don't understand why it wasn't originally, so if you find out, please let me know

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