Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Johanes

Need help with splitting string and making it into decimal

Recommended Posts

Posted · Original PosterOP
Public Class Form1
    Public listProdAndPrice As New List(Of String)
    Public totalPrice As New Decimal
    Public splitValue As New Decimal

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnAddToList.Click
        lsbProdAndPrice.Items.Clear()

        listProdAndPrice.Add(txbProdName.Text & " - P" & txbProdPrice.Text)
        For Each lsbItems In listProdAndPrice
            lsbProdAndPrice.Items.Add(lsbItems)
            splitValue = 

        Next


    End Sub
End Class

(Code is not complete)

im just stuck with this problem Q.Q i want to split a string from a list and take the value that i wanted and turn it into a decimal so i can compute it into its total

 

(Here is what my form looks like)

cashierProject.PNG.15b82e4078c6f30dcafbe0a885396261.PNG

my main problem is i want to split a string and then turn a string into a decimal.

 

 

here is an example of the form when its populated.

example.PNG.5aa45acd807626ef2ca188caed038fa6.PNG

Link to post
Share on other sites

You can fuss around with regex, splitting strings, checking input, etc.

Or... You are better off making a new class.

 

This looks like Visual Basic, which I am not totally familiar with, but with C# I would just simply make a new class called 'Product' with two (or more) properties. The name and price properties, which would be string and int/double respectively.

Then when a user creates a new product, you simply make a new instance of this class, provide the price and product name (of course checking if price is actually a number) and then simply add this to the list (make it a list of Products, not a list of strings anymore). Now you can display the price, the name, etc. but when the user clicks on it to pay, you can simply 'ask' this Product's (class') price.

 

If you don't want to bother doing that, you should - as mentioned above - do some stuff with splitting strings and such. If you want help with that, please provide some more context on how the whole string will look in action.


I apologize for the way I am. If my post seemed rude, that was not my intention. Just my ineptness in forming a nice coherent message.

"those times will never come back :("

"I wish I could find a way to end the pain, other than the way I keep thinking of"

Link to post
Share on other sites
Posted · Original PosterOP
3 minutes ago, minibois said:

You can fuss around with regex, splitting strings, checking input, etc.

Or... You are better off making a new class.

 

This looks like Visual Basic, which I am not totally familiar with, but with C# I would just simply make a new class called 'Product' with two (or more) properties. The name and price properties, which would be string and int/double respectively.

Then when a user creates a new product, you simply make a new instance of this class, provide the price and product name (of course checking if price is actually a number) and then simply add this to the list (make it a list of Products, not a list of strings anymore). Now you can display the price, the name, etc. but when the user clicks on it to pay, you can simply 'ask' this Product's (class') price.

 

If you don't want to bother doing that, you should - as mentioned above - do some stuff with splitting strings and such. If you want help with that, please provide some more context on how the whole string will look in action.

well the string would look like this "Apple - P20.00" in the list.. so i want to split this string to "Apple - P" "20.00" and then take the "20.00" and turn it into a decimal so that i can total it...

Link to post
Share on other sites
5 minutes ago, Johanes said:

well the string would look like this "Apple - P20.00" in the list.. so i want to split this string to "Apple - P" "20.00" and then take the "20.00" and turn it into a decimal so that i can total it...

In that situation I would create a for-loop that starts at the right end of the word and move back. Then I would keep going until it found the P (assuming the P is the currency sign and always appears in front of the money?). That way you know for sure you don't split it on the wrong thing.

 

Although I would still recommend the specific Product class approach.


I apologize for the way I am. If my post seemed rude, that was not my intention. Just my ineptness in forming a nice coherent message.

"those times will never come back :("

"I wish I could find a way to end the pain, other than the way I keep thinking of"

Link to post
Share on other sites
Posted · Original PosterOP
2 minutes ago, minibois said:

In that situation I would create a for-loop that starts at the right end of the word and move back. Then I would keep going until it found the P (assuming the P is the currency sign and always appears in front of the money?). That way you know for sure you don't split it on the wrong thing.

 

Although I would still recommend the specific Product class approach.

yeah i would love to know how to do that class thing as well.. but we haven't discussed that in our school yet..

 

the code already has a for loop statement in it. its just the taking the string and making it into decimal thats giving me a hard time >.>

Link to post
Share on other sites
1 minute ago, minibois said:

In that situation I would create a for-loop that starts at the right end of the word and move back. Then I would keep going until it found the P (assuming the P is the currency sign and always appears in front of the money?). That way you know for sure you don't split it on the wrong thing.

 

Although I would still recommend the specific Product class approach.

I would just update a global variable with the total as you add / remove stuff, then put the item name and price into the text box. 

 

Much more efficient as there would be no need for any loops


                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

i would just split the string on the "-" character, then trim spaces on both segments , then take all 0-9 and dot or , characters (some countries may use comma instead of dot) and put them in another string until you encounter an invalid character

ex

 

"Apple - P20.00" =>  0: "Apple "  1: " P20.00"

 

trim => 0: "Apple"  1: "P20.00"

 

backwards until not 0..9 or dot

 

number = "20.00"

 

there's conversion functions built in to convert valid string to float, currency etc

 

But in an ideal world, you should hold the data into an array with 2 properties per entry , ex name="apple"  price = 2000 (store number internally without decimals, just multiply by 100 or whatever) and separate what's in the list on screen by what's in that array.

You can use listIndex property or something like that when user clicks in the list to look up in the array and take from there the name and price.

The reason for this is because you may want to add more stuff in the future and then you'd have to edit the code in several places every time a feature is added.

Also, you may have a very long product name which may be wider than the list width, and then maybe you'd want to truncate it so that you'd see the price on the right side.

 

Btw... note that you can have list controls with two columns, so you could have the price in a column which has fixed width and the product name in the other column (which may truncate the text automatically if too long)

 

Link to post
Share on other sites
Posted · Original PosterOP

thanks for all the replies.. i am still rapping my head with this and all this new ideas are helping me greatly.

Link to post
Share on other sites
29 minutes ago, vorticalbox said:

I would just update a global variable with the total as you add / remove stuff, then put the item name and price into the text box. 

 

Much more efficient as there would be no need for any loops

For sure, splitting strings would be a last resort kind of thing. But it really depends on the complexity and function of the code.

If for example you added another one of the same product, does it go on a separate line? Add one more and double the price? etc. Those are times when you want the price to be separate for example..

But assuming this is some school code, this would probably suffice too.

27 minutes ago, mariushm said:

But in an ideal world, you should hold the data into an array with 2 properties per entry , ex name="apple"  price = 2000 (store number internally without decimals, just multiply by 100 or whatever) and separate what's in the list on screen by what's in that array.

You can use listIndex property or something like that when user clicks in the list to look up in the array and take from there the name and price.

The reason for this is because you may want to add more stuff in the future and then you'd have to edit the code in several places every time a feature is added.

Also, you may have a very long product name which may be wider than the list width, and then maybe you'd want to truncate it so that you'd see the price on the right side.

 

Btw... note that you can have list controls with two columns, so you could have the price in a column which has fixed width and the product name in the other column (which may truncate the text automatically if too long)

This seems like a good idea too, to have the price in a separate column. On top of it looking neater, it's also easier to do math with it.

What I find the issue with splitting the string at the dash is that if add an item that costs 10 currency, with the name "Apple - Apple 10" for example, this programs would format that as "Apple - Apple 10 - P10", when split you have to specifically call the last item in that array. Otherwise if you just assume the price is always within the second part, it will screw up.

 

But that just further points to what I said before in my post. It really depends on how complex this code should be. What I mentioned before is something to keep in mind for a full program, not a 'simple' homework assignment.

 


I apologize for the way I am. If my post seemed rude, that was not my intention. Just my ineptness in forming a nice coherent message.

"those times will never come back :("

"I wish I could find a way to end the pain, other than the way I keep thinking of"

Link to post
Share on other sites
36 minutes ago, Johanes said:

yeah i would love to know how to do that class thing as well.. but we haven't discussed that in our school yet..

 

the code already has a for loop statement in it. its just the taking the string and making it into decimal thats giving me a hard time >.>

Looks like the Val or Double.Parse methods are used in VB for this.

If you have "20.00" separate from the rest as a string, you can use something like this:

double output = Double.Parse(input);

 


I apologize for the way I am. If my post seemed rude, that was not my intention. Just my ineptness in forming a nice coherent message.

"those times will never come back :("

"I wish I could find a way to end the pain, other than the way I keep thinking of"

Link to post
Share on other sites

Maybe you can do the loop, but also call sql...use sql functions to do your conversion work ?  I'm guessing you are connected to a sql server ?  I don't know the functions in sql server...

 

Public Function ConnectToSQL() As String
    Dim con As New SqlConnection
    Dim reader As SqlDataReader
    Try
        con.ConnectionString = "Data Source=(local);Initial Catalog=TestDatabase;Persist Security Info=True;User ID=sa;Password=afm"
        Dim cmd As New SqlCommand("SELECT some_fancy_functions(" || input_string || ") FROM dual", con)
        con.Open()
        Console.WriteLine("Connection Opened")

        ' Execute Query    '
        reader = cmd.ExecuteReader()
        While reader.Read()
            Console.WriteLine(String.Format("{0}, {1}", _
               reader(0), reader(1)))
            'NOTE: (^^) You are trying to read 2 columns here, but you only        '
            '   SELECT-ed one (username) originally...                             '
            ' , Also, you can call the columns by name(string), not just by number '

        End While
    Catch ex As Exception
        MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
    Finally
        con.Close() 'Whether there is error or not. Close the connection.    '
    End Try
    'Return reader { note: reader is not valid after it is closed }          '
    Return "done"
End Function
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×