Divide By Zero Error Encountered In Vb.net
New Topic/Question Reply 6 Replies - 6373 Views - Last Post: 20 February 2012 - 10:51 AM Rate Topic: #1 Nuclearf1sh New D.I.C Head Reputation: 0 Posts: 2 Joined: 19-February 12 Error: Attempted to divide by zero. Posted 19 February 2012 - 05:02 PM I'm writing a program that returns the change after the user inputs the money given and purchase amount. It will show how many dollars, quarters, dimes, nickels, and pennies a customer would get back. I get an error after entering the numbers though, "Attempted to divide by zero". What am I doing wrong and how do I fix this?Forgot to post my code =/ Dim decAmountGiven As Decimal Dim decPurchaseAmount As Decimal Dim change As Decimal If String.IsNullOrEmpty(txtAmountGiven.Text) Or String.IsNullOrEmpty(txtPurchaseAmount.Text) Then MsgBox("Please enter a number") ElseIf txtAmountGiven.Text < txtPurchaseAmount.Text Then MsgBox("You have not given enough money") Else decAmountGiven = txtAmountGiven.Text decPurchaseAmount = txtPurchaseAmount.Text decAmountGiven = txtAmountGiven.Text decPurchaseAmount = txtPurchaseAmount.Text change = (decAmountGiven - decPurchaseAmount) lblMoneyReturn.Text = change lblDollarsOutput.Text = change \ 1 change = change Mod 1 lblQuartersOutput.Text = change \ 0.25 change = change Mod 0.25 lblDimesOutput.Text = change \ 0.1 change = change Mod 0.1 lblNickelsOutput.Text = change \ 0.05 change = change Mod 0.05 lblPenniesOutput.Text = change End If Is This A Good Question/Topic? 0 Back to top MultiQuote Quote + Reply Replies To: Error: Attempted to divide by zero. #2 smohd Critical Section Reputation: 1822 Posts: 4,627 Joined: 14-March 10 Re: Error: Attempted to divide by zero. Posted 19 February 2012 - 05:22 PM Try to change those \ with / because \ is an integer devider. Read this post here Was This Post Helpful? 1 Back to top MultiQuote Quote + Reply #3 woodjom D.I.C Addict Reputation: 29 Posts: 549 Joined: 08-May 08 Re: Error: Attempted to divide by zero. Posted 19 February 2012 - 06:14 PM To be technical, WTH would you use a back slash (\) for division? Even basic logic describes the 4 basic operators for math (+, -, *, /) Make sure you syntax check your code, since you had a rampant case of backslash (\) through you math. Was This Post Helpful? 0 Back to top MultiQuote Quote + Reply #4 Ch
The Decimal data type in VB.NET 3 Option Infer 4 How a VB.NET Structure Is Different From a… 5 Coding the Double Linked List "The Hard Way" About.com About Tech Visual Basic Quick Tips for VB Programmers NaN, Infinity, and Divide by Zero in VB.NET VB.NET Constants and Structured Error Handling. By Dan Mabbutt Visual Basic Expert Share Pin Tweet Submit Stumble Post Share By Dan Mabbutt Beginning programming books usually include this warning: "Don't divide by zero! You'll get a runtime error!"But things have changed in VB.NET! And, although there are http://www.dreamincode.net/forums/topic/267337-error-attempted-to-divide-by-zero/ more programming options (and the calculation is more accurate) it isn't always easy to see why things happen the way they do. In this article, we learn how to handle division by zero using VB.NET's structured error handling. And along the way, we also cover the new VB.NET constants: NaN, Infinity and Epsilon.If you run a 'divide by zero' scenario in VB.NET, you get http://visualbasic.about.com/od/quicktips/qt/DivZeroNaN.htm this result: Dim a, b, c As Double a = 1 : b = 0 c = a / b Console.WriteLine( _ "Have math rules" _ & vbCrLf & _ "been repealed?" _ & vbCrLf & _ "Division by zero " _ & vbCrLf & _ "must be possible!") So what's going on here? The answer is that VB.NET actually gives you the mathematically correct answer. Mathematically, you can divide by zero, but what you get is "infinity". Dim a, b, c As Double a = 1 : b = 0 c = a / b Console.WriteLine( _ "The answer is: " _ & c) ' Displays: ' The answer is: infinity continue reading below our video What is Virtual Reality? The value "infinity" isn't too useful for most business applications. (Unless the CEO is wondering what the upper limit on his stock bonus is.) But it does keep your applications from crashing on a runtime exception like less powerful languages do.VB.NET gives you even more flexibility by even allowing you to perform calculations. Check this out: Dim a, b, c As Double a = 1 :
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation https://msdn.microsoft.com/en-us/library/8syw9cx3.aspx APIs and reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. MSDN Library MSDN Library MSDN Library MSDN Library Design Tools Development Tools and Languages Mobile and Embedded Development .NET Development Office development Online Services Open Specifications patterns & practices Servers and Enterprise Development Speech Technologies divide by Web Development Windows Desktop App Development TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. Division by zero (Visual Basic Error) Other Versions Visual Studio 2008 Visual Studio 2005 The value of an expression used as a divisor is zero.To divide by zero correct this errorCheck the spelling of variables in the expression. A misspelled variable can implicitly create a numeric variable initialized to zero.Check previous operations on variable in the expression, especially those passed into the procedure as arguments from other procedures.See AlsoError Types (Visual Basic) Show: Inherited Protected Print Export (0) Print Export (0) Share IN THIS ARTICLE Is this page helpful? Yes No Additional feedback? 1500 characters remaining Submit Skip this Thank you! We appreciate your feedback. Dev centers Windows Office Visual Studio Microsoft Azure More... Learning resources Microsoft Virtual Academy Channel 9 MSDN Magazine Community Forums Blogs Codeplex Support Self support Programs BizSpark (for startups) Microsoft Imagine (for students) United States (English) Newsletter Privacy & cookies Terms of use Trademarks © 2016 Microsoft © 2016 Microsoft