decimal property

image_pdfimage_print
   
  

using System;

class MainEntryPoint {
    static void Main(string[] args) {
        Money cash1 = new Money();
        cash1.Amount = 40M;
        Console.WriteLine("cash1.ToString() returns: " + cash1.ToString());
        Console.ReadLine();
    }
}
class Money {
    private decimal amount;

    public decimal Amount {
        get {
            return amount;
        }
        set {
            amount = value;
        }
    }
    public override string ToString() {
        return "$" + Amount.ToString();
    }
}

   
     


Manually create a decimal number

image_pdfimage_print

   
 
/*
C#: The Complete Reference 
by Herbert Schildt 

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

// Manually create a decimal number. 
  
using System;  
  
public class CreateDec {     
  public static void Main() {     
    decimal d = new decimal(12345, 0, 0, false, 2); 
 
    Console.WriteLine(d); 
  }     
}


           
         
     


Compute the initial investment needed to attain a known future value given

image_pdfimage_print

   
 
/*
C#: The Complete Reference 
by Herbert Schildt 

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

/* Compute the initial investment needed to attain 
   a known future value given annual rate of return 
   and the time period in years. */ 
  
using System;  
  
public class IntialInvestment {     
  public static void Main() {     
    decimal InitInvest; // initial investment 
    decimal FutVal;     // future value 
 
    double NumYears;    // number of years  
    double IntRate;     // annual rate of return as a decimal 
  
    string str;  
 
    Console.Write("Enter future value: "); 
    str = Console.ReadLine(); 
    try {  
      FutVal = Decimal.Parse(str);  
    } catch(FormatException exc) {  
      Console.WriteLine(exc.Message);  
      return;  
    }  
 
    Console.Write("Enter interest rate (such as 0.085): "); 
    str = Console.ReadLine(); 
    try {  
      IntRate = Double.Parse(str);  
    } catch(FormatException exc) {  
      Console.WriteLine(exc.Message);  
      return;  
    }  
 
    Console.Write("Enter number of years: "); 
    str = Console.ReadLine(); 
    try {  
      NumYears = Double.Parse(str);  
    } catch(FormatException exc) {  
      Console.WriteLine(exc.Message);  
      return;  
    }  
 
    InitInvest = FutVal / (decimal) Math.Pow(IntRate+1.0, NumYears);  
  
    Console.WriteLine("Initial investment required: {0:C}", 
                      InitInvest);  
  }     
} 



           
         
     


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

image_pdfimage_print

/*
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.

image_pdfimage_print

   
 
/*
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

image_pdfimage_print
   
 
/*
 * 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

image_pdfimage_print

   
 
/*
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); 
  }    
}