6

I'm trying to get numbers formatted in this specific format:

"1 234.56"

So, two decimals, separated by a dot. And grouping thousands with a space char (optionally a single quote). The input values will never be larger than 9999.99.

I tried using patterns fo this, and even playing around with locales, but to no avail.

Marc Diethelm
  • 1,182
  • 1
  • 11
  • 15
  • From my knowledge you cannot specify a pattern that prints a white space after every 3rd character. I found something similar on CodeRanch but it's not very pretty: http://www.coderanch.com/t/292958/JSP/java/fmt-formatNumber-grouping-separator. Maybe it helps – Raul Rene Feb 26 '13 at 15:23

1 Answers1

9

Here is one way.

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="val" value="9999.99" /> 
<fmt:formatNumber value="${val}" pattern="#,###.##" var="pat" /> 
${fn:replace(pat, ",", " ")}
rickz
  • 4,324
  • 2
  • 19
  • 30
  • So basically string manipulation can be used in JSTL, I wasn't aware of that. I got it to work in my use case. Thanks!! In case it helps anyone, what I did in the end is more or less like this: ${fn:replace(pat, ",", ".")} I used an apostrophe as the thousands separator... Edit: Dang, I thought code blocks in comments were possible! – Marc Diethelm Feb 27 '13 at 10:58