Decimal Value is Zero when it should be 0.0x

元气小坏坏 提交于 2019-12-13 02:08:06

问题


If this was previously talked about, I'm sorry, I had a hard time searching on this.

I am calculating a depreciation rate. One portion of our calculation is 1/life in months. My table stores this data in a decimal field. I tried test = 1 / estimatedLife; but the result of the calculation of test (which is defined as a decimal) is 0.

Say the estimated life is 36 months. So 1/36 should equal 0.02777778.

Any thoughts of what I am doing wrong?

BTW, I changed the test to a double and had the same result.


回答1:


try:

test = 1.0M / estimatedLife;



回答2:


Another built-in alternative is Decimal.Divide:

test = Decimal.Divide(1, estimatedLife);

More to write, but pretty explicit I'd say.




回答3:


Your code divides two integers, then assigns the result (which is also an integer) to a decimal variable.

You need to switch to decimal division by making at least one of the operands a decimal, like this: 1.0M / estimatedLife.




回答4:


estimatedLife is an int, isn't it. Try this:

    decimal test = 1 / (decimal) estimatedLife;

or use SwDevMan81's suggestion:

    test = 1.0M / estimatedLife;

The issue is that integer division throws away everything after the decimal point. One of the arguments has to be a decimal for you to get the result you want.

The documentation link that he posted in a comment says:

If you want a numeric real literal to be treated as decimal, use the suffix m or M

so 1.0M means "a literal of type decimal with the value 1.0"




回答5:


just to add, if you require a specific precision (I noticed your output was set to 8 decimal places) you can use

decimal test = Math.Round(1M / estimatedLife,8);


来源:https://stackoverflow.com/questions/2449990/decimal-value-is-zero-when-it-should-be-0-0x

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