Double.toString without the exponential notation for large values
Question
In my JSF2 application, I want my double value to be displayed without exponential notation. Is it possible?
I cannot use NumberFormat
or DecimalFormat
since its going to change my data type to String.
I understand from Java documentation, if my double value is less than 10^-3 or greater than or equal to 10^7, then it is represented in so-called "computerized scientific notation"(Ref: http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#toString()).
And, In JSF while displaying a double value it will internally convert any data type to string.
For example, if my double value is 9999999999
it is getting displayed as 9.9e^9
. I want it to be displayed as it is. i.e 9999999999
But, it there any possibility to override this character of Double or is there any other approach to solve this issue?
Solution
If your code provides a double or Double, you can display that value directly in the page using a converter:
<h:outputText value="#{myBean.someDoubleValue}">
<f:convertNumber pattern="0"/>
</h:outputText>
OTHER TIPS
The correct solution for problems like this is to pass the internal data type around for as long as possible. Just at the moment when you display it to the user, you format it properly using DecimalFormat
.
So you have to find all the places in your JSF2 UI where you display doubles and use the proper formatters there.
Note that DecimalFormat
isn't threat safe, so you have to create a new formatter for each request (at least).