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?
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?
Share Improve this question edited Apr 27, 2012 at 12:08 Erick Robertson 33.1k13 gold badges71 silver badges98 bronze badges asked Mar 2, 2011 at 16:31 kamacikamaci 75.2k72 gold badges244 silver badges371 bronze badges 10- 1 But ... JSP is a server-side thing too ... – Pointy Commented Mar 2, 2011 at 16:34
- "implement it at my jsp file with JQuery or JavaScript" -- it appears you are confused: JSP runs on the server (in fact it is piled into standard Java code) while JavaScript runs on the client/browser, and jQuery is just a library built using JavaScript. – casablanca Commented Mar 2, 2011 at 16:39
- 1 @casablanca JavaScript actually can run on the server, but you're right in that's not what @kamaci is going for. He wants to write some JavaScript into his JSP to be run on the client, or to do it in JSP alone. – corsiKa Commented Mar 2, 2011 at 16:41
- 1 @casablanca thats what I want to do. @Pointy JavaScript runs on client side so I want to format it at client side. – kamaci Commented Mar 2, 2011 at 16:41
- 1 @glowcoder you exactly explained what I mean – kamaci Commented Mar 2, 2011 at 16:43
5 Answers
Reset to default 6Use 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./jsp/jstl_format_formatnumber_tag.htm
<%@ taglib prefix="fmt" uri="http://java.sun./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!