Csharp/CSharp Tutorial/Data Type/decimal Calculation

Материал из .Net Framework эксперт
Версия от 12:18, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Use the decimal type to compute a discount.

using System;  
  
class UseDecimal {     
  public static void Main() {     
    decimal price;  
    decimal discount; 
    decimal discounted_price;  
  
    price = 19.95m;  
    discount = 0.25m; // discount rate is 25% 
 
    discounted_price = price - ( price * discount);  
  
    Console.WriteLine("Discounted price: $" + discounted_price);  
  } 
}
Discounted price: $14.9625

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

using System; 
   
class Example { 
  public static void Main() { 
    decimal amount;   
    decimal rate_of_return;  
    int years, i;   
   
    amount = 100000.0M; 
    rate_of_return = 0.17M; 
    years = 100; 
 
    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);  
  } 
}
Original investment: $100000.0
Rate of return: 0.17
Over 100 years
Future value is $658546088583.68156535520269753