Float string to integer string

I have a float in the String strFloat. I wanted to convert it to a rounded integer also stored in a String. This looks awful to me, so please offer some suggestions for improvement.

String strInteger = new Integer(Float.valueOf(strFloat).intValue()).toString()

• Could you please clarify where the float comes from and what is stored in this variable. Jul 25, 2013 at 4:55
• Float.valueOf("2.99").intValue() != Math.round(Float.valueOf("2.99")) You sure you mean rounded integer? Jul 25, 2013 at 7:07

Instead of creating a new Integer and throwing it away, use String.valueOf() directly.

String strInteger = String.valueOf(Float.valueOf(strFloat).intValue())

• -1 for using float/double in a most likely precise context. Jul 24, 2013 at 10:00
• @mnhg: Then the OP needs to mention that context. This could also just be interfacing with legacy data, or a simple "stringified" application. Additionally, that thing is already called "float". Jul 24, 2013 at 13:37
• @Bobby Actually it's the other way around. Maybe I'm to critical/pessimistic but I had to deal with to many bad designed interfaces etc. If there is no reason to be inprecise and your language supports a better suitable type, you definitely should go with this type. It will make your live easier. Somebody at sometime will copy/reuse your code, decide that he need a decimal digit or two and it will go down from there. (Only calling it float says nothing. It is a String.) Jul 25, 2013 at 4:32

If your float is already a String then you definitely should go with BigDecimal. Creating a real intermediate float only add inaccuracy.

String strInteger = new BigDecimal(strFloat).toBigInteger().toString());


Of course you can take advantage of all the arithmetic power of BigDecimal and define a specific rounding mode etc.

EDIT: Just to prove that the float approach will fail (tested in Java 6):

Float.valueOf( "1.99999999" ).intValue()==2
Float.valueOf( "2.9999999" ).intValue()==3
Float.valueOf( "393650.99" ).intValue()==393651
Float.valueOf( "2545818.9" ).intValue()==2545819
(and many more)

• If the String was created from a float, exact precision is already maintained: "How many digits must be printed for the fractional part of m or a? There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type float." docs.oracle.com/javase/7/docs/api/java/lang/… Jul 24, 2013 at 21:08
• Float.valueOf( "1.99999999" ).intValue()==2 (Java 6) Jul 25, 2013 at 5:06
• You can't generate "1.99999999" from a Java float. Aug 4, 2013 at 20:43
• There is no use in arguing about it without knowing the context. Maybe it is a float from the database, or a webservice or was converted to string long ago and cut to 10 characters. Expect the worst, especially if it doest cost "anything". Aug 5, 2013 at 7:17
• Without knowing the context, it's hard to say that creating a lot of intermediate BigDecimals and BigIntegers doesn't cost anything. Aug 18, 2013 at 13:55