Discussions

Web tier: servlets, JSP, Web frameworks: JSP output double with 2 point precision?

  1. JSP output double with 2 point precision? (3 messages)

    I need to print out prices to html in a page, and of course doubles such as $1.90 print out as $1.9, any easy to way enforce the precision to be printed?

    thanx,

    wall
  2. JSP output double with 2 point precision?[ Go to top ]

    U can try this
    <%
    NumberFormat formatter = NumberFormat.getInstance();
    formatter.setMaximumFractionDigits(1);
    formatter.setMinimumFractionDigits(1);
    %>
    in the html o/p u can show as
    <td>$<%=formatter.format("the variable name here");%></td>

    Don't forget to import java.text

    Hope it helps


  3. There is an even simpler DecimalFormat class that allows you to give output patterns like #.## or #.00
  4. JSP output double with 2 point precision?[ Go to top ]

    easiest way for currency display....

    NumberFormat.getCurrencyInstance().format(1.90) will return $1.90

    NumberFormat is in java.text.