none
How can I compute a discount in Visual Basic Form Application? RRS feed

  • Question

  • Example, (In Visual Basic Form Application)

    There's a total of something. and I typed in a 20% or 20 in a text box labelled as discount.

    What's the code for that? thanks in advance for answering!

    Wednesday, February 22, 2017 1:31 PM

Answers

  • That's it, How can I convert the text to a double? 

    Take a look at the Double.Parse method.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Proposed as answer by Neda ZhangModerator Thursday, February 23, 2017 7:54 AM
    • Marked as answer by Ememow Thursday, February 23, 2017 1:35 PM
    Wednesday, February 22, 2017 2:35 PM
    Moderator
  • That's it, How can I convert the text to a double? 

    Hi Ememow,

    Thank you for posting here.

    Use the Double.TryParse Method(String, Double) to do the parsing to avoid getting an exception if the parsing fails:

    Dim value As Double
    If Double.TryParse(TextBox1.Text, value) Then
      ' parsing worked, so use the value in here
    Else
      ' parsing failed, so alert the user to that fact
    End If

    Best Regards,

    Neda Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Neda ZhangModerator Thursday, February 23, 2017 8:04 AM
    • Marked as answer by Ememow Thursday, February 23, 2017 1:33 PM
    Thursday, February 23, 2017 8:03 AM
    Moderator

All replies

  • Convert the text to a double and then do the math.

    What part are you having trouble with?

    We are all likely to assume this is homework based on the nature of the question so we'll help you through it but won't give the answer outright.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Wednesday, February 22, 2017 1:52 PM
    Moderator
  • That's it, How can I convert the text to a double? 
    Wednesday, February 22, 2017 2:26 PM
  • That's it, How can I convert the text to a double? 

    Take a look at the Double.Parse method.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Proposed as answer by Neda ZhangModerator Thursday, February 23, 2017 7:54 AM
    • Marked as answer by Ememow Thursday, February 23, 2017 1:35 PM
    Wednesday, February 22, 2017 2:35 PM
    Moderator
  • That's it, How can I convert the text to a double? 

    Hi Ememow,

    Thank you for posting here.

    Use the Double.TryParse Method(String, Double) to do the parsing to avoid getting an exception if the parsing fails:

    Dim value As Double
    If Double.TryParse(TextBox1.Text, value) Then
      ' parsing worked, so use the value in here
    Else
      ' parsing failed, so alert the user to that fact
    End If

    Best Regards,

    Neda Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Neda ZhangModerator Thursday, February 23, 2017 8:04 AM
    • Marked as answer by Ememow Thursday, February 23, 2017 1:33 PM
    Thursday, February 23, 2017 8:03 AM
    Moderator
  • You can see both Neda and Reeed did not make much programs for invoices. 

    The Double is not sufficient because it gives values behind the decimal point which are not real to trust. 

    Also you have to round to away from zero because otherwise you get banking rounding which is standard in .Net and not used for retail on most places on earth.

    Dim x = "99,55" 'the decimalpoint seperator according to your language
    Dim y = CDec(x)
    Dim discount = Math.Round(y * 0.2, 2, MidpointRounding.AwayFromZero)

     


    Success
    Cor

    Thursday, February 23, 2017 8:51 AM
  • You can see both Neda and Reeed did not make much programs for invoices. 

    The Double is not sufficient because it gives values behind the decimal point which are not real to trust. 

    Also you have to round to away from zero because otherwise you get banking rounding which is standard in .Net and not used for retail on most places on earth.

    Dim x = "99,55" 'the decimalpoint seperator according to your language
    Dim y = CDec(x)
    Dim discount = Math.Round(y * 0.2, 2, MidpointRounding.AwayFromZero)

     


    Success
    Cor

    You should reduce your level of assumption. This is HOMEWORK so it is up to the person posting to figure out those details.  The point was to show them the Parse method - they need to figure out the most correct answer on their own.

    Giving them the complete answer outright helps no one.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, February 23, 2017 12:39 PM
    Moderator

  • You should reduce your level of assumption. This is HOMEWORK so it is up to the person posting to figure out those details. 

    How did you assume it was homework. Now I see what he answers I think that as well.

    :-)

    In real business with accounting software is the rounding always the problem when calculating a discount and then it is always the same that persons start using the double, while you never get that correct. 


    Success
    Cor

    Thursday, February 23, 2017 2:42 PM

  • You should reduce your level of assumption. This is HOMEWORK so it is up to the person posting to figure out those details. 

    How did you assume it was homework. Now I see what he answers I think that as well.

    :-)

    In real business with accounting software is the rounding always the problem when calculating a discount and then it is always the same that persons start using the double, while you never get that correct. 


    Success
    Cor

    A decade of using this forum has given me a pretty good handle on spotting homework questions.  :)

    Of course there is a more appropriate data type for currency values.  I would have to assume that this point was covered in the course being taught.  I generally use the da Vinci principle when answering homework questions... give them enough information to lead them in the right direction but ensure that a blind copy of the information, taken with no additional thought, will not yield a completely correct result.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, February 23, 2017 3:01 PM
    Moderator
  • Hi Reed (and Neda),

    I told you both where probably not affected with accounting software. Reed told he was. That is not a better qualification. Those from who Ray, Tommy and John (mr. Monkeyboy) are part of had much more fun in this business. What is nicer than doing scientific programming instead of boring accounting software which is endless times done. 

    I guess Frank, Les and Paul are part of the same boring software developing I was in. (Although I had also some fun projects).

     


    Success
    Cor




    • Edited by Cor Ligthert Thursday, February 23, 2017 11:28 PM
    Thursday, February 23, 2017 11:22 PM