문제

So I'm making a cookie clicker clone in java (shame on me) and for one thing, I need the double to increase by .1 for each cursor you own. The code looks like this (cookies is a double, and all other values are ints):

cookies = cookies + (cursors*.1+grandmas/2+farms*4+factories*10);

however, when this outputs, it outputs numbers like 2.399999999999999 instead of 2.4. What is going on?

도움이 되었습니까?

해결책

That is double's precision. You could use

cookies = Math.round( cookies * 100.0 ) / 100.0;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top