Problem getting answers into decimal and calculating

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1239 Views - Last Post: 11 February 2009 - 11:41 AM Rate Topic: -----

#1 Buffalo999111  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 26-January 09

Problem getting answers into decimal and calculating

Post icon  Posted 10 February 2009 - 07:47 AM

I'm trying to get the answer "dblcost" to go into 2 decimal spots. Sometimes it does, sometimes it doesnt.

Also, I have to create something that continually adds each transaction to lblfinalcost. Mine seems to be a little buggy. Right not I have strfinalcost = strginalcost + dblcost. I don't know why it's all weird.

Another question i have is how do I make a number default into the textbox? I want it to say 1, but be able for the user to delete it and then replace it with their own number.

Any help would be appreciated. I made a different topic a few days ago, but this is probably more specific since I made some changes.

Public Class Form1

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'populate listbox
		lstproduct.Items.Add("Paper, @ $10.95")
		lstproduct.Items.Add("Flash Drive, @ $25.00")
		lstproduct.Items.Add("Pencil, @ $0.25")

		
		Exit Sub

	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Close()
	End Sub

	Private Sub btncompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncompute.Click

		'declare variables
		Dim strproduct As String
		Dim intquantity As Integer
		Dim dblcost As Double
		Static strfinalcost As String
		Dim i As Integer
		

		'input from screen
		intquantity = (txtquantity.Text)


		'string location
		strproduct = lstproduct.Items.Item(i)


		'Error message for not selecting an item
		i = lstproduct.SelectedIndex
		If i = -1 Then
			MsgBox("Please select an item", MsgBoxStyle.OkOnly, "Error")
			Exit Sub
		End If

		
		'error message  <=0

		If intquantity <= 0 Then
			MsgBox("Please enter a number greater or equal to 1", MsgBoxStyle.OkOnly, "Error")
			Exit Sub
		End If

		'compute cost for product
		If i = 0 Then
			dblcost = dblcost + 10.95 * intquantity

		ElseIf i = 1 Then
			dblcost = dblcost + 25.0 * intquantity


		ElseIf i = 2 Then
			dblcost = dblcost + 0.25 * intquantity


		End If


		strfinalcost = strfinalcost + dblcost

		'compute depending if payment is in Cash
		If radcash.Checked Then
			dblcost = dblcost * 0.98
		End If



		'message box
		MsgBox("The cost is $" & dblcost, MsgBoxStyle.OkOnly, "Curent cost")

		lblfinalcost.Text = "The total cost of your order is $" & strfinalcost

		'final cost string
		strfinalcost = String.Format("{0:c}", dblcost)

	End Sub
End Class


Is This A Good Question/Topic? 0
  • +

Replies To: Problem getting answers into decimal and calculating

#2 sam.adams61  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 283
  • Joined: 14-July 08

Re: Problem getting answers into decimal and calculating

Posted 10 February 2009 - 08:15 AM

As for setting the textbox to display a default value of, say, 1...you can do so programmatically in the form's load event like so:

[code] Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles MyBase.Load

TextBox1.Text = 1
End Sub
End Class

As for the rest of your code, I'm looking through it, but some of the more experienced guys might be better equipped to answer. Anyway, I'll see if I can come up with something...hope this helps in the mean time. ;)
Was This Post Helpful? 1
  • +
  • -

#3 sam.adams61  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 283
  • Joined: 14-July 08

Re: Problem getting answers into decimal and calculating

Posted 10 February 2009 - 08:28 AM

Read throught the following code carefully...

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Close()
	End Sub

	Private Sub btncompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncompute.Click

		'declare variables
		Dim strproduct As String
		Dim intquantity As Integer
		Dim dblcost As Double
		Static strfinalcost As String
		Dim i As Integer
		

		'input from screen
		intquantity = (txtquantity.Text)


		'string location
		strproduct = lstproduct.Items.Item(i)


		'Error message for not selecting an item
		i = lstproduct.SelectedIndex
		If i = -1 Then
			MsgBox("Please select an item", MsgBoxStyle.OkOnly, "Error")
			Exit Sub
		End If



There's a real blunder here Buffalo.... Why are you exiting the procedure after displaying the MsgBox, and still in the 'If End If' statement?

This post has been edited by sam.adams61: 10 February 2009 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#4 Buffalo999111  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 26-January 09

Re: Problem getting answers into decimal and calculating

Posted 10 February 2009 - 08:33 AM

Im not sure. Just beginner mistake i guess? Are you saying I shouldn't end the sub at all? I just removed it and it doesn't appear to even do a thing!
Was This Post Helpful? 0
  • +
  • -

#5 sam.adams61  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 283
  • Joined: 14-July 08

Re: Problem getting answers into decimal and calculating

Posted 10 February 2009 - 08:41 AM

View PostBuffalo999111, on 10 Feb, 2009 - 02:33 PM, said:

Im not sure. Just beginner mistake i guess? Are you saying I shouldn't end the sub at all? I just removed it and it doesn't appear to even do a thing!

Sorry, my fault...keep it in.
Was This Post Helpful? 0
  • +
  • -

#6 Buffalo999111  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 26-January 09

Re: Problem getting answers into decimal and calculating

Posted 10 February 2009 - 08:43 AM

Well maybe it does do something I didn't notice. I'm sure you know more than me about it.
Was This Post Helpful? 0
  • +
  • -

#7 sam.adams61  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 283
  • Joined: 14-July 08

Re: Problem getting answers into decimal and calculating

Posted 10 February 2009 - 09:15 AM

In your code you have:
 'input from screen
		   intquantity = (txtquantity.text)

You need to change this in order to perform the necessary type conversion...change it to:
 intquantity = Val(txtquantiy.text)

Hope this helps!!

This post has been edited by sam.adams61: 10 February 2009 - 09:16 AM

Was This Post Helpful? 0
  • +
  • -

#8 Buffalo999111  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 26-January 09

Re: Problem getting answers into decimal and calculating

Posted 10 February 2009 - 09:28 AM

Yep that appears to of solved it. All that's left is the continuous total at the end. I static'd the strfinalcost so that it would stay the same. And then I did
strfinalcost = strfinalcost + dblcost

What did I do wrong? Isn't strfinalcost supposed to stay put and then keep adding the dblcost calculations?
Was This Post Helpful? 0
  • +
  • -

#9 sam.adams61  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 283
  • Joined: 14-July 08

Re: Problem getting answers into decimal and calculating

Posted 10 February 2009 - 09:33 AM

As far as getting dblcost into two decimal places is concerned you should find that this is the case, at least in your text property of the lblfinalcost. About getting it to show two decimal places in the message box, well, it does do that...providing the figure is two whole digits and not a zero. I think you want the final zeros to show as well. Is that right? I'm not sure how to achieve that, but I hope and trust that what I've given you will prove to be a little help at least. BTW, the 'Exit Sub' doesn't really do anything.... Best practices, I think, would advise to leave it out.

[quote name='Buffalo999111' date='10 Feb, 2009 - 01:47 PM' post='538538']
I'm trying to get the answer "dblcost" to go into 2 decimal spots. Sometimes it does, sometimes it doesnt.
Was This Post Helpful? 0
  • +
  • -

#10 sam.adams61  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 283
  • Joined: 14-July 08

Re: Problem getting answers into decimal and calculating

Posted 10 February 2009 - 09:48 AM

Also, I'm not too sure about the following line in your code:

strfinalcost = strfinalcost + dblcost


since strfinalcost has no value before this point you're not actually adding anything to it...whereas, if you simply put
strfinalcost = dblcost
you're passing the value of dblcost to strfinalcost. Either way it accomplishes the same thing...I'm just not sure that it's necessary for you to do it the way that you did. Anyway, all the best with your assignment. Take care.
Was This Post Helpful? 0
  • +
  • -

#11 Buffalo999111  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 26-January 09

Re: Problem getting answers into decimal and calculating

Posted 10 February 2009 - 10:17 AM

Well doing it that way was the way we were taught to do it. We want to add dblcost to finalcost to get a continuous running total. Im still unclear why it isn't working completely for me.
Was This Post Helpful? 0
  • +
  • -

#12 Buffalo999111  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 26-January 09

Re: Problem getting answers into decimal and calculating

Posted 10 February 2009 - 03:20 PM

So anyone know what I'm talking about? This is the only thing that's tripping me up now. I need to calculate a running total of the transactions
Was This Post Helpful? 0
  • +
  • -

#13 sam.adams61  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 283
  • Joined: 14-July 08

Re: Problem getting answers into decimal and calculating

Posted 10 February 2009 - 04:18 PM

Sorry I can't help here with this one...bound to be someone on DIC who can though.... The best of Irish luck, my friend! ;)
Was This Post Helpful? 0
  • +
  • -

#14 Buffalo999111  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 26-January 09

Re: Problem getting answers into decimal and calculating

Posted 10 February 2009 - 06:53 PM

Thanks. I have it working somewhat, but obviously I did something wrong. It doesn't calculate correctly. Theres gotta be a simple fix a beginner like me is missing.
Was This Post Helpful? 0
  • +
  • -

#15 south73paw  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 88
  • Joined: 06-October 08

Re: Problem getting answers into decimal and calculating

Posted 11 February 2009 - 04:01 AM

The first thing that springs to mind is that your variable strfinalcost should really be declared as a numeric data type. Ideally, this and dblcost should be declared with the Decimal data type - this is usually used for monetary values.

You can then convert textbox values that are stored in the variables.

e.g. Dim decCost as Decimal = CDec(txtCost.Text)
Dim decFinalCost as Decimal

Then, when the calculations are done and you're ready to display the result, convert decFinalCost back to a string

e.g. txtFinalCost.Text = CStr(decFinalCost)

This code worked for me as a quick trial - user inputs value into first text box - clicks button - value input is added to running total and displayed in second text box. This was all in the button's Click event.

		'User inputs cost price into textbox
		Dim decCost As Decimal = CDec(txtCost.Text)

		'Keeps running total of all numbers every time button clicked
		'Static keyword guarantees that previous values stored in variable are not removed, but added to
		Static decRunningTotal As Decimal

		'Add user input to value, if any, stored in running total
		decRunningTotal = decRunningTotal + decCost

		'Display running total in 2nd textbox
		txtRunningTotal.Text = CStr(decRunningTotal)
		'Clear first textbox for new user input
		txtCost.Clear()


Is this what you were after?

In terms of why your addition statement didn't work...your basic structure was fine (strfinalcost = strfinalcost + dblcost), but you were trying to perform addition with a number and a string instead of two numbers (which can be converted back to a string for display in a text box). If you have Option Strict turned on, it makes you do the conversion.

Hope that helps anyway.

:)
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2