Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleCalculate escalating royalties in Visual Basic .NET
DescriptionThis example shows how to calculate escalating royalties in Visual Basic .NET.
Keywordsescalating royalties, royalties, calculate, percent, FormatCurrency, GetPercent, percent, VB.NET
CategoriesAlgorithms, Strings
 
This example calculates escalating royalties. For example, an author might earn 10 percent of net receipts on the first 2,000 copies sold, 12 percent on the next 2,000 copies, 15 percent on the next 2,000 copies, and 17 percent on any additional copies sold.

This program lets you enter the royalty percentages, the number of books at each royalty percentage, and book's price each (the net price the publisher receives per book), and the number of books sold. When you click the Calculate button, the following code executes.

The code gets the price each price and number of copies sold. It then calls ProcessLevel to process the copies sold at each royalty level. This example only allows four levels.

After it has calculated the royalties at each level, the code displays the total royalties in the label lblTotalRoyalty.

 
Private Sub btnCalculate_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnCalculate.Click
    ' Get basic info.
    Dim royalty As Single = 0
    Dim price_each As Single = CSng(txtPriceEach.Text)
    Dim total_copies As Long = CLng(txtCopiesSold.Text)
    Dim total_royalties As Single = 0

    ProcessLevel(price_each, txtRoyalty0, _
        txtCopies0, lblRoyalty0, _
        total_copies, total_royalties)
    ProcessLevel(price_each, txtRoyalty1, _
        txtCopies1, lblRoyalty1, _
        total_copies, total_royalties)
    ProcessLevel(price_each, txtRoyalty2, _
        txtCopies2, lblRoyalty2, _
        total_copies, total_royalties)
    ProcessLevel(price_each, txtRoyalty3, _
        txtCopies3, lblRoyalty3, _
        total_copies, total_royalties)

    txtTotalRoyalty.Text = FormatCurrency(total_royalties)
End Sub
 

Subroutine ProcessLevel processes the royalties at a royalty level. First it gets the number of copies at that level. If the number possible is greater than the remaining number of books sold, it sets the number of copies equal to the remaining number of books.

The code uses the GetPercent function to get the royalty percentage for this level. It multiplies the number of copies at this level by the price each and the royalty percent to get the value of this level's royalties. The program uses FormatCurrency to display the amount in a label and subtracts the number of copies at this level from the remaining number of copies.

 
' Process royalties for this level.
Private Sub ProcessLevel(ByVal price_each As Single, ByVal _
    txt_royalty As TextBox, ByVal txt_copies As TextBox, _
    ByVal lbl_royalty As Label, ByRef total_copies As Long, _
    ByRef total_royalties As Single)
    ' Get the number of copies at this level.
    Dim num_copies As Long = _
        CLng(txt_copies.Text)
    If num_copies > total_copies Then _
        num_copies = total_copies

    ' Get the royalty percentage.
    Dim royalty_percentage As Single = _
        GetPercent(txt_royalty.Text)

    ' Calculate the royalty on these copies.
    Dim new_royalty As Single = _
        price_each * num_copies * royalty_percentage

    ' Display the result.
    lbl_royalty.Text = FormatCurrency(new_royalty)
    total_royalties += new_royalty
    total_copies -= num_copies
End Sub
 
Function GetPercent converts a string into a Single. If the string contains a percent sign, the function removes it and divides the number by 100. For example, it converts the string "10%" into 0.10.
 
' Convert a value that may have
' a % sign into a Single.
Private Function GetPercent(ByVal txt As String) As Single
    ' See if it contains a %.
    If InStr(txt, "%") > -1 Then
        ' Divide by 100.
        Return Single.Parse(txt.Replace("%", "")) / 100
    Else
        Return Single.Parse(txt)
    End If
End Function
 
Note: This example converts text into numbers by using CLng and CSng. Normally I use Long.Parse and Single.Parse for that purpose but those methods do not understand dollar signs and commas in numbers as in $1,000.00 while CLng and CSng do.
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated