Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

c# - Using integer instead of decimal

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...