I'm stumped on an exercise problem at the end of a chapter on my book. An earlier problem had me create an algorithm to calculate compound interest (this is using C# by the way). That was simple enough. A few questions later, it mentioned how some other languages don't have the decimal type and to modify the previous algorithm so that all monetary values are of type int. Seemed like a simple enough problem, converting the dollar amounts to pennies, etc. But I can't get the calculation to output what it needs to.

Any help? I've been looking at this problem for too long now lol

using System;

public class Interest
{
   public static void Main( string[] args )
   {
      int amount; // amount on deposit at end of each year
      int principal = 100000; // initial amount before interest
      double rate = 0.05; // interest rate

      // display headers
      Console.WriteLine( "Year{0,20}", "Amount on deposit" );

      // calculate amount on deposit for each of ten years
      for ( int year = 1; year <= 10; year++ )
      {
         // calculate new amount for specified year
         amount = principal * ( Math.Pow (1 + rate, year));

         // display the year and the amount
         Console.WriteLine( "{0,4}{1,20:C}", year, amount );
      } // end for
   } // end Main
} // end class Interest

I think I figured it out. I just cast the whole expression "(product * (Math.Pow(1 + rate, year))" to int. That way it did the calculations, and then cast it to int, so the rate didn't get truncated.

Now, I'm stuck on the second part. We had to display the amounts as the dollars and cents, separated by a decimal. I used amount/100 for the dollars, and ammount%100 for the cents. But when it out puts, it doesn't show two digits cents that have a 0 for the second number. Is there a way to fix that with any sort of format?

If you have a better way to do this problem, please let me know.

Thanks!

You could use String.Format(System.Globalization.Culture.CurrentCulture, "{0:C}", Math.Round(amount / 100.0, 2)); The format command sets the string conversion culture to your current culture, it then says, "My first parameter is of type currency", and then you round the amount / 100 to two decimal places.

Please take care with rounding errors =)

Another way, if you don't want to use culture based currency conversion... String.Format("${0}.{1:00}", amount / 100, amount%100) {1:00} indicates that this parameter must always have at least 2 digits. If amount%100 = 1, it would print 01. If amount%100 = 10, it will print 10.

Comments
Keep up the good work.

Another way, if you don't want to use culture based currency conversion... String.Format("${0}.{1:00}", amount / 100, amount%100) {1:00} indicates that this parameter must always have at least 2 digits. If amount%100 = 1, it would print 01. If amount%100 = 10, it will print 10.

Thanks for the reply! That helps a lot.

This second one is more what I probably should roll with. Only because we are still pretty early in the C# book and course, and we haven't gotten to the culture based stuff yet.

This article has been dead for over six months. Start a new discussion instead.