July 2011
Intermediate to advanced
400 pages
8h 46m
English
Numbers are just as easy to use as strings. For example, let’s suppose you want to calculate the selling price or grand total of some item based on its pretax value or subtotal. To do this, you would need to multiply the subtotal by the applicable tax rate and add the result to the value of the subtotal. Assuming the subtotal to be $100 and the tax rate to be 17.5 percent, this Ruby program does the calculation and displays the result:
4calctax.rb
subtotal = 100.00
taxrate = 0.175
tax = subtotal * taxrate
puts "Tax on $#{subtotal} is $#{tax}, so grand total is $#{subtotal+tax}"Obviously, this program would be more useful if it could perform calculations on a variety of subtotals rather than calculating the same value time after time! Here ...
Read now
Unlock full access