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()
  • \$\begingroup\$ Could you please clarify where the float comes from and what is stored in this variable. \$\endgroup\$ – mheinzerling Jul 25 '13 at 4:55
  • \$\begingroup\$ Float.valueOf("2.99").intValue() != Math.round(Float.valueOf("2.99")) You sure you mean rounded integer? \$\endgroup\$ – abuzittin gillifirca Jul 25 '13 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())
| improve this answer | |
  • \$\begingroup\$ -1 for using float/double in a most likely precise context. \$\endgroup\$ – mheinzerling Jul 24 '13 at 10:00
  • 2
    \$\begingroup\$ @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". \$\endgroup\$ – Bobby Jul 24 '13 at 13:37
  • \$\begingroup\$ @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.) \$\endgroup\$ – mheinzerling Jul 25 '13 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)
| improve this answer | |
  • \$\begingroup\$ 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/… \$\endgroup\$ – Olathe Jul 24 '13 at 21:08
  • \$\begingroup\$ Float.valueOf( "1.99999999" ).intValue()==2 (Java 6) \$\endgroup\$ – mheinzerling Jul 25 '13 at 5:06
  • \$\begingroup\$ You can't generate "1.99999999" from a Java float. \$\endgroup\$ – Olathe Aug 4 '13 at 20:43
  • \$\begingroup\$ 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". \$\endgroup\$ – mheinzerling Aug 5 '13 at 7:17
  • \$\begingroup\$ Without knowing the context, it's hard to say that creating a lot of intermediate BigDecimals and BigIntegers doesn't cost anything. \$\endgroup\$ – Olathe Aug 18 '13 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.