Skip to main content
added 299 characters in body
Source Link
mheinzerling
  • 2.7k
  • 15
  • 17

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 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.

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)
edited body
Source Link
mheinzerling
  • 2.7k
  • 15
  • 17

If your float is already a String thatthen you definitely should go with BigDecimal. Creating a real intermediatedintermediate 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.

If your float is already a String that you definitely should go with BigDecimal. Creating a real intermediated 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.

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.

Source Link
mheinzerling
  • 2.7k
  • 15
  • 17

If your float is already a String that you definitely should go with BigDecimal. Creating a real intermediated 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.