Using integer instead of decimal

这一生的挚爱 提交于 2019-12-13 08:19:53

问题


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.



来源:https://stackoverflow.com/questions/15392160/using-integer-instead-of-decimal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!