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()
2 Answers 2
Instead of creating a new Integer
and throwing it away, use String.valueOf()
directly.
String strInteger = String.valueOf(Float.valueOf(strFloat).intValue())
-
\$\begingroup\$ -1 for using float/double in a most likely precise context. \$\endgroup\$mheinzerling– mheinzerling2013年07月24日 10:00:55 +00:00Commented 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– Bobby2013年07月24日 13:37:04 +00:00Commented 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\$mheinzerling– mheinzerling2013年07月25日 04:32:53 +00:00Commented 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)
-
\$\begingroup\$ If the
String
was created from afloat
, 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– Olathe2013年07月24日 21:08:00 +00:00Commented Jul 24, 2013 at 21:08 -
\$\begingroup\$
Float.valueOf( "1.99999999" ).intValue()==2
(Java 6) \$\endgroup\$mheinzerling– mheinzerling2013年07月25日 05:06:29 +00:00Commented Jul 25, 2013 at 5:06 -
\$\begingroup\$ You can't generate "1.99999999" from a Java float. \$\endgroup\$Olathe– Olathe2013年08月04日 20:43:07 +00:00Commented 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\$mheinzerling– mheinzerling2013年08月05日 07:17:44 +00:00Commented Aug 5, 2013 at 7:17
-
\$\begingroup\$ Without knowing the context, it's hard to say that creating a lot of intermediate
BigDecimal
s andBigInteger
s doesn't cost anything. \$\endgroup\$Olathe– Olathe2013年08月18日 13:55:03 +00:00Commented Aug 18, 2013 at 13:55
Explore related questions
See similar questions with these tags.
Float.valueOf("2.99").intValue() != Math.round(Float.valueOf("2.99"))
You sure you mean rounded integer? \$\endgroup\$