**Click Star to Rate Post**

Its actually easy to do basic math in Visual Basic 5.0/6.0 and Visual Basic.NET 2002/2003 and Visual Basic 2005. Both the VB 5.0/6.0 and .NET Framework has a Math Class/Module to perform more advanced calculations. Unfortunately I do not know much beyond the scope of basic Math calculations, so don’t expect anything beyond the basics.

**Update:** *Since many are apparently wanting to make a calculator based application I added a new link to a Tutorial for VB .NET on making a basic calculator. The link is with the other VB calculator example links near the bottom of this post.*

**Addition/Subtraction/Multiplication/Division**

Operators for Basic Math

- + = Addition
- – = Subtraction
- * = Multiply
- / = Division *ForwardSlash is More Exact, Includes Decimal Remainders
- \ = Division *BackSlash Returns Integers only with No Decimals/Remainders

To perform basic math is easy with any version of VB. You can perform Math on variables and/or actual numbers…

**Note:** *In Visual Basic 5.0 and Visual Basic 6.0 you should change the .NET Codes in this article from Integer types to Longs.*

Dim myMath As Integer myMath = 2 + 2 MsgBox(myMath)

The Messagebox will return the number ‘4’ since it added ‘2 + 2?. Adding actual variables it just as easy…

Dim myMath As Integer Dim myMath2 As Integer myMath = 2 myMath2 = 5 MsgBox(myMath * myMath2)

The first message with the forward slash includes the remainder or decimal portion and the back slash will return the integer number ‘2’ with no decimal. So, the first messagebox returns = ‘2.4’ and the Second message box returns = ‘2’…

Subtraction was just as easy. Just use the Dash – operator as the example does above. The message box above returns = ‘7’ since ’12 – 5 = 7?.

You can also perform calculations on String variable types. You use them similar to the number based types. I would recommend using the Val() function in Visual Basic. Val will return or convert the Integers in a String to Doubles that is better to do when you are working with numbers as values to be calculated. Even if the String contains a non-number value along with a number value, Val() will try to convert the characters to a number value. Example…

Dim myMath As String Dim myMath2 As String myMath = "12k" myMath2 = "5j" MsgBox(Val(myMath) * Val(myMath2))

The messagebox above will return a ’60’ since Val() only cared about the Interger characters and not any non-number based values.

If your interested, I have both a VB 6.0 and VB.NET 2005/2008 Example of how to make a basic calculator application. Click Here for the Classic VB Example and Click Here for the VB.NET 2005/2008 Example.

**Update:** Click this link if you want to view a tutorial on specifically making a basic calculator via Visual Basic .NET.

As you can see, performing basic math is very easy to do. There are other Functions in Visual Basic as well like Sqr (Sqrt in .NET), Sin, Cos, and more. But that is beyond the scope of this article. Maybe at another time. Anyways, Have Fun!

Jason

*Revised: 2014*

scooljpls can you send me the step to create math calculator. pls!!!!!!!!!!!!!!!!!

JasonPost authorI have a example of a basic calculator for .NET and VB 6.0 at my vbcodesource.com site. Just go to the examples page for which vb your using.

Jason

Joegreat article:

what if i already have a value in a database,say 2 and i add 4 so that after saving the changes,the value held within the database is 6..how does on implement this?

how do handle the 2 already in the database and the new value 4?

ardiehow to make a multiplication and division without using * and / as signs…?

with using loop… can u give examples ? ?

thank you. .

ardiepls email me about the examples i really need it

thank for the concern

shraddhahey, can u tell me total steps to create calculator because i have no knowledge of vb.net.. but i am trying to learn it..!!

JasonPost authorHi, I have a pretty decent calculator example at my sister site: vbcodesource.com. I’m updating the download links on this article to the updated ones.

Here is the download link: http://www.vbcodesource.info/downloads/vbNet2005/basicCalculatorExampleNet05.zip

It even has some features that the regular Windows calculator didn’t have until Microsoft updated it in Windows 7.

Hope this helps. 🙂

Jason

ObedPls i am running VB 8 express edition but the calculator codes seem not to be working why?

JasonPost authorYou need to provide info on what your problem is.

Soahibhow i handle dot in textbox , it will more than one time in texbox an give mathematicall error

Fr. LovelaceHow do you multiply a string and a decimal using textboxes please.

JasonPost authorFR, just use the text property of the textbox controls.

Dim result As Double

result = Val(Textbox1.Text) * Val(Textbox2.Text)

JasonPost authorSoahib, I somehow missed your comment. If your still in need of help I suggest you restrict the textbox to only allow numbers with a single decimal point. Click the link below to see the code to do that.

http://www.vbcodesource.info/313/allowingrestricting-certain-characterslettersnumbers-in-a-textbox-control/

chrisI am wanting to build a random number math game for my kids. two textboxes, new numbers button(generates random numbers for textboxes), a comboBox to choose operation( ,-,*), a textBox for them to enter thier answer, and a pictureBox to display a smiley face or frown(depending on the answer they give is correct) i am not good at generating code. any help would be appreciated. Chris

JasonPost authorHi Chris, check out the links below to get started on generating random numbers. I’m guessing your using VB.NET?

http://www.freevbcode.com/ShowCode.asp?ID=4451

http://www.vbdotnetheaven.com/UploadFile/mahesh/stringvbnet11052005021335AM/stringvbnet.aspx

That should help you get started. 🙂

Jason

amanhello jason, i’m still new with vb.net, and now i need to do the webpage using vb.net, currently i’m using the VS2012,

its bit complicated to do the calculating..can you show me example of coding of vb.net that involve calculating using database??really need your helps..:)

DavidHi Jason. I’m using VB6, and have made a small standalone media player, which works quite well – even playing mpeg videos (using the MMC player). However, when I try to play the video with your mciSendString example, it doesn’t work. Basically I want to play the video in a picture box instead of an external window, which the MMC control won’t do. But when I click on the link to your full tutorial (http://www.vbcodesource.info/?p=155) all I get is an empty page.

Any hope you can help me move along? I’d appreciate it!

Thanks

jaymiea subtractio code that will result to divison answer…

sample:

code 3 – 3 = but a result is 1 which is division..

plsss help me..

give a code…

i really need it now