Hello Jan,
do you know a function to display a Big Decimal as scientific notation with 3 decimals e.g. 1,47e-10 ?
Thank you!
Nino
Hi Nino,
well, BigDecimal is a powerful class but its not that easy to achieve your wish. Anyway, I searched a bit through the open space of the internet and prepared a little script for you.
It shows various existing ways to convert a BigDecimal to a string (toString, toPlainString, toEngineeringString) and implements a method that forces Scientific notation.
Hope that helps!
Jan
Thank you Jan!!!
Just some additional hints why it makes sense to use BigDecimal in Java script and not double values as supported by Java Script. As discussed here, a simple statement like
0.2 + 0.4
will give 0.6000000000000001 and not as expected 0.6. Knowing the technical details about double values helps to understand the reason but not the result. Also strange is that in this example
var x =(0.0001*0.01) alert(x) alert("value " + x);
you will see 1.0000000000000002E-6 at first and "value 0.0000010000000000000002" in the second dialog. Reason is that in the first situation the double value is passed to the Java world and visualized by Java, in second case the double value is converted to a string in JS and that string is passed to Java to be display. So its also related to how a value is represented / printed in a certain language.
You may use
0.020000000000000004.toFixed(3);
for example to get "0.020" or follow the ideas in this article or simply stick with Java's BigDecimal.
Note that with 20.2 we will offer a simple "parseBigDecimal" function to simplify the entrance to the BigDecimal world.
Happy Scripting
Just to mention that this parseBigDecimal is now available with 2020 R2.0
If you have a Javascript Number object (i.e. a double value), then you can get the scientific notation very easily with the following method:
myDoubleValue.toExponential(2)
See background article here.