如何在c#中获得0到1之间的值。 [英] How to get value between 0 to 1 in c#.

查看:375
本文介绍了如何在c#中获得0到1之间的值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码: -



I've the following code:-

int p, t, n = 1;
               decimal r;

               p = Convert.ToInt32(txtLAmount.Text);
               r = Convert.ToDecimal(lblroibuss.Text);
               t = Convert.ToInt32(txtLyear.SelectedItem.Value);

               if (lblprocfees.Text == "Annually")
               { n = 1; }
               else if (lblprocfees.Text == "Quarterly")
               { n = 4; }
               else if (lblprocfees.Text == "Monthly")
               {
                   n = 12;
               }

               n = n * t;

               double R = Convert.ToDouble(1 + (r / 400));
               double Ramt1 = Convert.ToDouble( p * (Math.Pow(R, n) - 1));
               double Ramt2 = Convert.ToDouble(1 - Math.Pow(R, -1 / 3));
               double amt = Ramt1 / Ramt2;
               decimal a = Convert.ToDecimal( Math.Round(10000 * amt) / 10000);







但是当我编译的时候Ramt1和2得到0值,因为计算错误。请帮助,如果你能...




But when I compile the Ramt1 and 2 are getting 0 values due to which the calculation is giong wrong. Please help if you can...

推荐答案

当你写1/3时,它是一个整数除法,结果是0.0。要进行浮点除法,至少有一个操作数必须是一个浮点。



另外,你不应该没有理由混合小数和双。转换越少越好。
When you write 1 / 3, it is an integer division and this the result is 0.0. To make floatting point division, at least one of the operand must be a floatting point.

Also, you should not mix decimal and double without reason. Less conversion is better.


首先使用调试器。

在线上放置一个断点

Start by using the debugger.
Put a breakpoint on the line
n = n * t;

并查看值是什么。

逐步执行代码,在每个阶段计算出您希望在每行之前得到的内容执行。

如果你得到yopu的期望,继续前进。

如果你不这样做,你可以开始看看你为什么不这样做。



我们不能为你做这件事,因为我们不知道你的输入会有什么明智的价值,或任何关于这应该做什么的线索。没有它,我们就会盲目工作。



所以试试吧 - 调试是一项你需要用来正确开发的技能(就像所有其他技能一样),它是最好的在一小段代码上开发它而不是尝试一个巨大的代码!

And look at what the values are.
Step through the code, at each stage working out what you expect to get before each line is executed.
If your get what yopu expect, move on.
If you don't, you can start looking at exactly why you don't.

We can't do this for you, because we have no idea what "sensible values" would be for your inputs, or any clue as to what this is supposed to do. Without that, we would be working blind.

So try it - debugging is a skill which you have to use to develop properly (like all other skills) and it's best to develop it on a small piece of code instead of trying on a huge one!


只是调试你的项目,你会发现你的问题
just debug your project, you will find your problem


这篇关于如何在c#中获得0到1之间的值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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