c# - Math.Round problems -



c# - Math.Round problems -

i'm trying create clock game. hours , seconds both float values using math.round round them off nearest whole number. problem hours , seconds variables aren't changing @ all. using math.round wrong?

public void update() { hours = (float)math.round(hours, 0); clocktime = hours + ":" + seconds; if (hours >= 24) hours = 0; if (seconds >= 60) seconds = 0; }

in update method day/night class.

float elapsed = (float)gametime.elapsedgametime.totalseconds; clock.hours += (float)elapsed; clock.update();

when print numbers on screen, nil changing. if take away (float) cast math.round error cannot convert double float.

don't utilize floating point in case, there's absolutely no reason hour, min or sec non-integral.

what's happening you're ending float value 59.9999 despite fact think you're rounding it.

there real dangers in assuming floating point values have more precision do.

if hold number of seconds in unsigned integral 32-bit type, can represent elapsed time until year 2150 ad, should still playing game @ point :-)

then utilize integer calculations work out hours , seconds (assuming you're not interested in minutes seems case), pseudo-code such as:

hours = elapsed_secs / 3600 secs = elapsed_secs % 3600 print hours ":" seconds

beyond advice, you're doing seems tad strange. adding elapsed seconds field (which assume you're checked isn't set zero) hours variable. that's going create gameplay little hard time speeds @ 3 , half one thousand times normal rate.

c#

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -