Wednesday, August 15, 2012

How to make a simple tax (VAT) calculator

Well, I promised a tutorial. Let's get started!
We'll be building a calculator for calculating tax. If you take a look at How to calculate VAT you will find some information on VAT and also the formula for calculating it.
Firstly, here is the code:
puts "Welcome to VAT Calculator."
puts "Let me remind you to only enter numbers. Do not type any letters, currency signs or percentage signs!"
puts "Please enter the VAT rate."
vatRate = gets.chomp
puts "Thank you, you have entered a VAT rate of " + vatRate
puts "Do you wish to add VAT to a value or subtract it?"
puts "Enter the number 0 if you wish to add VAT to a value, or 1 if you wish to subtract VAT from a value."
operation = gets.chomp
if operation == "0"
 puts "You have selected to add VAT to a value."
 puts "Please enter the value you wish to add VAT to."
 mainVal = gets.chomp puts "VAT based on your entered value is " +  String((Float(mainVal) * Float(vatRate))/100)
puts "Your entered value with VAT added is " + String(Float(mainVal) + (Float(mainVal) * Float(vatRate))/100)
else
puts "You have selected to subtract VAT from a value."
puts "Please enter the value you wish to subtract VAT from."
mainVal = gets.chomp puts "VAT based on your entered value is " + String((Float(mainVal) * Float(vatRate))/100)
puts "Your entered value with VAT subtracted is " + String((Float(mainVal) - (Float(mainVal)*Float(vatRate))/100))
end
puts "Thank you for using the VAT Calculator."
gets.chomp

Secondly, I'm sorry, but blogger seems to not allow me to enter code a way that would actually make it pleasant to look at. This is the best I could do.

Thirdly, let's explain it!

puts "Welcome to VAT Calculator." - puts just prints out a string and goes to a new line.

vatRate = gets.chomp - this creates a new variable called vatRate and it waits for user input (gets) then it removes the 'enter key' (chomp). For instance, when you enter the number 23 you have to press enter to confirm it, but if we left out 'chomp' at the end, it would save the variable as 23enter, which we do not want.

if operation == "0" - if the variable operation is 0. It is in quotes because when we enter something with 'gets' we enter a string and not a number. So 0 is a number and "0" is a string.

String((Float(mainVal) * Float(vatRate))/100) - When you surround something with String() it tries to perceive it as a string. For instance String(20) will function like "20". Float() tries to take a string (let's say "23.42") and make it a Float (float is a number with decimals). This whole line is a formula for calculating VAT.

else - this can come after an 'if' statement. For instance 'if something is true, do this, ELSE do something else'.

end - this ends the 'if statement'

gets.chomp - at the last line, if we didn't have this, the application would close as soon as the calculations were done, and we would not be able to see any results. This makes the application wait until the user presses any key or just closes the application.

I hope this has been of some help,
have fun experimenting in Ruby!

In one of the future tutorials we will add a GUI (graphic user interface), so the calculator will look something like this: VAT Calculator

5 comments:

  1. Nice blog.
    Really, you are sharing great information from your blog that is very useful for me. Thank you so much.
    VAT Calculator

    ReplyDelete
  2. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.company formation services india

    ReplyDelete
  3. Grande calculatrice chère, vous partagez toujours la valeur.... TVA

    ReplyDelete