I have external hardware which will send me their values with scientific notation like this: 5.312E+03
However I receive this as a String and need to cast it to a BigDecimal.
doing this:
String value = "5.312E+03";
BigDecimal amount = new BigDecimal(String);
I get the following value 5312
. BigDecimal will remove the E+03
and also remove the dot from the original Value. And I was wondering why and how to force BigDecimal to not do that.
When I give the variable value
the following: 5.312
it will stay like this as BigDecimal.
Also just giving 5.312E
will naturally cause a NumberFormatException
Here is some StackTrace to show this Behaviour:
2017-06-13 09:05:13,713 TRACE [SCTLaboratoryDevice] [] (SCT) received data: 600+6.513E+03
2017-06-13 09:05:13,714 INFO [SCTLaboratoryDevice] [] (SCT) filtered value: 6.513E+03
2017-06-13 09:05:13,718 DEBUG [SCTLaboratoryDevice] [] (SCT) send WebSocket{amount=TcpAmountImpl [value=6513, unit=null]} on channel SCT
2017-06-13 08:57:34,351 TRACE [CTLaboratoryDevice] [] (SCT) received data: 600+6.513E+03
2017-06-13 08:57:34,351 INFO [SCTLaboratoryDevice] [] (SCT) filtered value: 6.513E
2017-06-13 08:57:34,352 SEVERE [SCTLaboratoryDevice] [] (SCT) null: java.lang.NumberFormatException
at java.math.BigDecimal.<init>(BigDecimal.java:596)
at java.math.BigDecimal.<init>(BigDecimal.java:383)
at java.math.BigDecimal.<init>(BigDecimal.java:806)
2017-06-13 08:56:28,260 TRACE [SCTLaboratoryDevice] [] (SCT) received data: 600+6.513E+03
2017-06-13 08:56:28,260 INFO [SCTLaboratoryDevice] [] (SCT) filtered value: 6.513
2017-06-13 08:56:28,260 DEBUG [SCTLaboratoryDevice] [] (SCT) send WebSocket {amount=TcpAmountImpl [value=6.513, unit=null]} on channel SCT
Do I have a mistake in my thoughts about BigDecimal and scientific notation or how do I make BigDecimal accept the original value 5.312E+03
correctly?
The BigDecimal has been created correctly.
5.312E+03 is the same value as 5312.
The only difference is how it is being displayed.