-6

I made a float that adds 1 to it every second using deltaTime then printing it out on the screen, I don't wan't to see all the numbers after the point.

float+=1*deltaTime;
//result is something like this 1.1149411, I instead want it to be just 1.
JackWhiteIII
  • 1,388
  • 2
  • 11
  • 25
Kevin Bryan
  • 1,846
  • 2
  • 22
  • 45

2 Answers2

2

There are two options:

  • casting: this will produce an unrounded result. For example: 2.7 will be converted to 2.

    int someInt = (int) someFloat;

  • rounding: the result will be rounded correctly.

    int someInt = Math.round(someFloat);

1

You could just use a cast to an int like so:

int integerNumber = (int) yourFloat;
JackWhiteIII
  • 1,388
  • 2
  • 11
  • 25
  • Thanks it worked but can you please explain me what is casting and how it works? – Kevin Bryan Jul 01 '15 at 15:23
  • Please see Paul's answer, it does explain more. – JackWhiteIII Jul 01 '15 at 15:24
  • @StormAsdg Please just Google it. You do not need to ask someone every time you have an incredibly basic question, your original post being another example. You need to learn how to help yourself. – tnw Jul 01 '15 at 15:26
  • @Storm Casting is very basic programming design. You would benefit more from a book or online tutorial, than some hand holding. – CubeJockey Jul 01 '15 at 15:27