使用整数而不是小数 [英] Using integer instead of decimal

查看:164
本文介绍了使用整数而不是小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在完成我的特定家庭作业时遇到了麻烦。这几乎是不可能的。问题是这样的......

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.

推荐答案

只是给你一个提示:

int amount;

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

这篇关于使用整数而不是小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆