2
\$\begingroup\$

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()
\$\endgroup\$
2
  • \$\begingroup\$ Could you please clarify where the float comes from and what is stored in this variable. \$\endgroup\$ Commented Jul 25, 2013 at 4:55
  • \$\begingroup\$ Float.valueOf("2.99").intValue() != Math.round(Float.valueOf("2.99")) You sure you mean rounded integer? \$\endgroup\$ Commented Jul 25, 2013 at 7:07

2 Answers 2

4
\$\begingroup\$

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

String strInteger = String.valueOf(Float.valueOf(strFloat).intValue())
\$\endgroup\$
3
  • \$\begingroup\$ -1 for using float/double in a most likely precise context. \$\endgroup\$ Commented Jul 24, 2013 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
    Commented Jul 24, 2013 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\$ Commented Jul 25, 2013 at 4:32
4
\$\begingroup\$

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)
\$\endgroup\$
6
  • \$\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
    Commented Jul 24, 2013 at 21:08
  • \$\begingroup\$ Float.valueOf( "1.99999999" ).intValue()==2 (Java 6) \$\endgroup\$ Commented Jul 25, 2013 at 5:06
  • \$\begingroup\$ You can't generate "1.99999999" from a Java float. \$\endgroup\$
    – Olathe
    Commented Aug 4, 2013 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\$ Commented Aug 5, 2013 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
    Commented Aug 18, 2013 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.