I use formatter for my double values in Java like:
private static final NumberFormat formatter = new DecimalFormat("###0.00");
formatter.format(doubleValue);
However I don't want to do it on the server side. How can I write an equivalent code in Javascript or JQuery?
Use the NumberFormatter plug-in for JQuery.
FYI: the old link was here.
I know this doesn't really answers your question, but have you thought of using the jstl formatNumber tag?
Use JSTL <fmt:formatNumber>
... http://www.tutorialspoint.com/jsp/jstl_format_formatnumber_tag.htm
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:formatNumber type="number" pattern="###0.00" value="${bean.value}" />
Trying using sprintf() for JavaScript.
Ext JS's Format class has a number
function you can use to do this, see Ext.util.Format. As of Ext 4 it also supports locale-specific "thousand separators".
I guess the downside is that you need to import part of the Ext framework to use it!
来源:https://stackoverflow.com/questions/5170465/what-is-the-equivalent-of-numberformat-in-javascript-or-jquery