0

I came accross a code snippet:

BigDecimal bigDecimal = new BigDecimal(454756.43);
float number = bigDecimal.floatValue();
System.out.println(number);

The result is: 454756.44

So basically I am trying to understand how to get the correct result?

  • 1
    Does this answer your question? [losing precision converting from java BigDecimal to double](https://stackoverflow.com/questions/5749615/losing-precision-converting-from-java-bigdecimal-to-double) – Giancarlo Romeo Apr 17 '20 at 12:41
  • 1
    What is a correct result in your opinion? And why? – Amongalen Apr 17 '20 at 12:41
  • Is there a way on how to get 454756.43 because that i think it should be right. If its wrong in your opinion please tell me why as i am still learning java this can help me with learning. – Rachit Gupta Apr 17 '20 at 12:44
  • float doesn't have enough precision to represent that number so it rounds it to the nearest possible. If you just want to print the number, use `bigDecimal.toString()`. If you actually want to use the number and care precision, then you have to work on `bigDecimal` itself, without extracting float from it. – Amongalen Apr 17 '20 at 12:48

0 Answers0