Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java convert float to integer

Tags:

java

I want to do an operation like this : if the given float numbers are like 1.0 , 2.0 , 3.0 , I want to save them to database as integer (1,2,3 ), if they are like 1.1 , 2.1 , ,3.44 , I save them as float. what's the best solution for this problem using java ? The corresponding field in database is type of varchar.

like image 531
Sawyer Avatar asked Feb 02 '10 09:02

Sawyer


1 Answers

Just try int i = (int) f;.

EDIT : I see the point in the question. This code might work :

 int i = (int) f;
 String valToStore = (i == f) ? String.valueOf(i) : String.valueOf(f);
like image 198
fastcodejava Avatar answered Oct 05 '22 13:10

fastcodejava