Decimal Value is Zero when it should be 0.0x

2019-07-24 19:18发布

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.

5条回答
家丑人穷心不美
2楼-- · 2019-07-24 19:22

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);
查看更多
成全新的幸福
3楼-- · 2019-07-24 19:31

Another built-in alternative is Decimal.Divide:

test = Decimal.Divide(1, estimatedLife);

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

查看更多
萌系小妹纸
4楼-- · 2019-07-24 19:38

try:

test = 1.0M / estimatedLife;
查看更多
Rolldiameter
5楼-- · 2019-07-24 19:43

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.

查看更多
小情绪 Triste *
6楼-- · 2019-07-24 19:47

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"

查看更多
登录 后发表回答