C# ++ and -- incrementing double var by 0.1
apologies if this has been asked, but I cannot find it.
I have a double variable which I am doing ++ and -- to increase and decrease the value.
My number is 95.8 for example, and the ++ would make it 96.8
But I would like the ++ 开发者_开发问答operator to increment by 0.1 each time, therefore the number would increase to 95.9 for example.
Thanks, Dan
That's not supported unless you create your own type (class) and override these operators.
Use the following instead:
x += 0.1;
By definition, the ++
and --
operators go by 1
.
You could create a class with the semantics you want, but you'll baffle future readers of your code.
Why fight the system? Why not just say x += 0.1;
etc.?
精彩评论