Using integer instead of decimal

2019-09-20 04:27发布

问题:

I'm having trouble with a particular homework assignment of mine. It almost seems impossible. The question goes like this...

"In the future, you may work with other programming languages that do not have a type like decimal which supports precise monetary calculations. In those languages, you should perform such calculations using integers. Modify the application to use only integers to calculate the compound interest. Treat all monetary amounts as integral numbers of pennies. Then break the result into its dollars and cents portions by using the division and remainder operations, respectively. Insert a period between the dollars and the cents portions when you display the results."

When I follow the directions and use integers I get these overflow errors before I can even divide anything out. Does anyone have any idea how to make this work? Here's the original code that needs to be modified...

        decimal amount; //amount on deposit at end of each year
        decimal principal = 1000; //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 *
                ((decimal)Math.Pow(1.0 + rate, year));

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

This is the code I have so far...

        long amount; //amount on deposit at end of each year
        long principal = 100000; //initial amount before interest
        long rate = 5; //interest rate
        long number = 100;

        //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 *
                ((long)Math.Pow(100 + rate, year));

            amount /= number;

            number *= 10;

            //display the year and the amount
            Console.WriteLine("{0,4}{1,20}", year, amount);

It gets some of the right numbers, but then starts spitting out negative numbers for some reason.

回答1:

Just to give you a hint:

int amount;

//...some code...
//let's pretend we have an amount of 100.97
amount = (int)(100.97 * 100); // amount = 10097


回答2:

Math.Pow uses double. It does not use long.
In the following line you are implictly casting your rate and year to double.

amount = principal *
    ((long)Math.Pow(100 + rate, year));

So you are effectly doing this:

double dRate = (double)(100 + rate);
double dYear = (double)year;
double dPow = Math.Pow(dRate, dYear);
amount = principal * (long)dPow;

If you want your Pow function to really use long then you probably need to write it yourself.