-2

Is there any function returns only the real number without the floating point? For an example, func(1.xxx)->1 func(52.xx)->52 func(0.xx)->0

Is there any function does so?

rds
  • 26,253
  • 19
  • 107
  • 134
Amrmsmb
  • 1
  • 27
  • 104
  • 226

2 Answers2

3

Simply casting to int would truncate everything past the decimal point.

float f1 = 10.345;
int i1 = (int) f1; // Gives 10

float f2 = 10.897;
int i2 = (int) f2; // Also gives 10
starf
  • 1,063
  • 11
  • 15
  • Cast to floor, `Math#round()` to... well... round. – Rogue Jun 14 '14 at 10:23
  • would you mind up-voting my question please – Amrmsmb Jun 14 '14 at 10:27
  • @Rogue That's correct for positive numbers. Negative numbers work a little differently. We are talking about truncating here. See http://math.stackexchange.com/questions/344815/how-do-the-floor-and-ceiling-functions-work-on-negative-numbers – starf Jun 14 '14 at 11:28
1

You can do :

double d = 100.675;

System.out.println((int) d);

this gives you 100.

System.out.println(Math.round(d));

gives you 101.

You can also use :

new java.text.DecimalFormat("#").format(10.0); // => "10"

now the choice is yours that how you want to do and main thing depend on that what is your expected output is.

user3145373 ツ
  • 7,858
  • 6
  • 43
  • 62