Search code examples
javascriptjsphtml-tablenumber-formatting

How to prevent 'E' when displaying double number in jsp?


I'm displaying number (of type double in java ) in HTML. And it is automatically converting it to E.

<td>${obj.val}</td>

when I look at the value in server side/debugger, the value is 13857542.994060002, but it displays the same value in the below format.

1.3857542994060002E7

I was wondering if there's a way to format it.


Solution

  • If you are going to display a fixed number of decimal places, you can use toFixed. For example, this outputs the number with two decimal places:

    13857542.994060002.toFixed(2); // outputs 13857542.99
    

    If you are going to format numbers to locale representation, you can use toLocaleString:

    13857542.994060002.toLocaleString('en-US'); // outputs 13,857,542.994