Use the decimal type to compute the future value of an investment

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
/*
Use the decimal type to compute the future value
of an investment.
*/

using System;

public class FutVal {
public static void Main() {
decimal amount;
decimal rate_of_return;
int years, i;

amount = 1000.0M;
rate_of_return = 0.07M;
years = 10;

Console.WriteLine(“Original investment: $” + amount);
Console.WriteLine(“Rate of return: ” + rate_of_return);
Console.WriteLine(“Over ” + years + ” years”);

for(i = 0; i < years; i++) amount = amount + (amount * rate_of_return); Console.WriteLine("Future value is $" + amount); } } [/csharp]

Use the decimal type to compute a discount.


   
 
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Use the decimal type to compute a discount. 
  
using System;  
  
public class UseDecimal {     
  public static void Main() {     
    decimal price;  
    decimal discount; 
    decimal discounted_price;  
  
    // compute discounted price 
    price = 19.95m;  
    discount = 0.15m; // discount rate is 15% 
 
    discounted_price = price - ( price * discount);  
  
    Console.WriteLine("Discounted price: $" + discounted_price);  
  }     
}
           
         
     


Using Decimals

   
 
/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 * Version: 1
 */
using System;

namespace Client.Chapter_1___Common_Type_System
{
  public class UsingDecimals
  {
    static void Main(string[] args)
    {
      decimal MyDecimal = 3.50m;
    }
  }

}

           
         
     


Compute the regular payments for a loan


   
 
/*
C# A Beginner&#039;s Guide
By Schildt

Publisher: Osborne McGraw-Hill
ISBN: 0072133295
*/
 /* 
   Project 2-3 
 
   Compute the regular payments for a loan. 
 
   Call this file RegPay.cs 
*/ 
 
using System; 
 
public class RegPay {    
  public static void Main() {    
    decimal Principal;    // original principal 
    decimal IntRate;      // interest rate as a decimal, such as 0.075 
    decimal PayPerYear;   // number of payments per year 
    decimal NumYears;     // number of years 
    decimal Payment;      // the regular payment 
    decimal numer, denom; // temporary work variables 
    double b, e;          // base and exponent for call to Pow() 
 
    Principal = 10000.00m; 
    IntRate = 0.075m; 
    PayPerYear = 12.0m; 
    NumYears = 5.0m; 
 
    numer = IntRate * Principal / PayPerYear; 
  
    e = (double) -(PayPerYear * NumYears); 
    b = (double) (IntRate / PayPerYear) + 1; 
 
    denom = 1 - (decimal) Math.Pow(b, e); 
     
    Payment = numer / denom; 
 
    Console.WriteLine("Payment is {0:C}", Payment); 
  }    
}


           
         
     


Convert a decimal to the binary format

   
 
//GNU General Public License version 2 (GPLv2)
//http://cbasetest.codeplex.com/license

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SDFL.Helper
{
    public class StrHelper
    {
        /// <summary>
        ///         Convert a decimal to the binary format.
        ///         like: 
        ///         stra = Convert.ToString(4, 2).PadLeft(8, &#039;0&#039;);
        ///         //stra=00000100
        /// </summary>
        /// <param name="DecimalNum"></param>
        /// <returns></returns>
        public static string ToBinary(Int64 decimalNum)
        {
            Int64 binaryHolder;
            char[] binaryArray;
            string binaryResult = "";

            // fix issue#5943: StrHelper.ToBinary(0)  result "", it should be 0
            if (decimalNum == 0)
            {
                return "0";
            }
            // end fix issue#5943

            while (decimalNum > 0)
            {
                binaryHolder = decimalNum % 2;
                binaryResult += binaryHolder;
                decimalNum = decimalNum / 2;
            }

            // rever the binaryResult, e.g. we get 1101111, then revert to 1111011, this is the final result
            binaryArray = binaryResult.ToCharArray();
            Array.Reverse(binaryArray);
            binaryResult = new string(binaryArray);
            return binaryResult;
        }    

    }
}