1

Why is my float automatically rounding and how do i get it to stop

  float pageCount = 10/6;

should be 1.666 but it is giving 1.0

smart boy
  • 671
  • 4
  • 11
  • 24

2 Answers2

8

Your calculation is being done in integer type since both the operands are of int type

cast or mark atleast one of the operand as float.

float pageCount = 10/6f; //6f specifying 6 as float

or

float pageCount = ((float) 10)/6;

In your current form, both operands are of integer type and their division results in integer value that is why you get 1 not 1.666

Habib
  • 219,104
  • 29
  • 407
  • 436
-1

Simple to the following 10.0/6

Anirudh
  • 387
  • 2
  • 9