BigDecimal.floatValue versus Float.valueOf

Posted by Frank on Programmers See other posts from Programmers or by Frank
Published on 2012-11-13T21:33:43Z Indexed on 2012/11/13 23:22 UTC
Read the original article Hit count: 234

Filed under:

Has anybody ever come across this:

System.out.println("value of: " + Float.valueOf("3.0f")); // ok, prints 3.0
System.out.println(new BigDecimal("3.0f").floatValue()); // NumberFormatException

I would argue that the lack of consistency here is a bug, where BigDecimal(String) doesn't follow the same spec as Float.valueOf() (I checked the JDK doc).

I'm using a library that forces me to go through BigDecimal, but it can happen that I have to send "3.0f" there. Is there a known workaround (BigDecimal is inaccessible in a library).

© Programmers or respective owner

Related posts about java