0

I got this line to format a number :

<fmt:formatNumber type="number" pattern="###,#######" value="1234567890" var="test"/>

Above line output 123 4567890 with my current locale (french).

If I use below code :

<c:set var="ref" value="123 4567890"/> ${ref == test}

It return false, why?

First, I thought that test was no a String hence I tried that :

<c:set var="test2" value="${test}"/>

But ${ref == test2} still return false.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
alain.janinm
  • 19,951
  • 10
  • 65
  • 112
  • Why don't you compare the actual model value instead of the locale-specific formatted human representation? – BalusC Jan 13 '15 at 14:57
  • @BalusC Yeah you're right. Of course, what I'm trying is a bit silly. Actually I don't really need it. I was just playing with `formatNumber` and `replace` methods and I stumbled upon that weird behaviour. I'm not looking for a better/different solution. I'm just trying to understand why this test fails, why it can't work. I'm probably missing something in the way data are stored/compared using jstl. – alain.janinm Jan 13 '15 at 15:09

1 Answers1

-1

Looks like the French locale uses a non-breaking space character (the "&nbsp", hex 0xA0 (160 decimal)), but you were using a regular space (character 32)

This should print true:

<fmt:formatNumber type="number" pattern="###,#######" value="1234567890" var="test"/>
<% pageContext.setAttribute("nbsp", String.valueOf('\u00a0'));%>
<c:set var="ref" value="123${nbsp}4567890"/>
ref is ${ref} <br />
test is ${test} <br />
${ref == test}  
Pradyumna
  • 1,583
  • 4
  • 19
  • 34
  • Please read my question, it's mentionned that it prints "123 4567890" – alain.janinm Jan 12 '15 at 12:25
  • Right. Given the circumstances, that's what I suspect may be an incorrect fact. Try a simple small JSP with only the lines that I posted to see if your problem still persists – Pradyumna Jan 12 '15 at 17:47
  • Yes still have it, when you execute that code it prints true? – alain.janinm Jan 12 '15 at 18:26
  • Ok, I guess I figured it out - looks like it's a nbsp, and not space – Pradyumna Jan 13 '15 at 04:43
  • It may have been something like that but no it still print false. If I inspect the page source, ${test} really contains a regular space. – alain.janinm Jan 13 '15 at 08:05
  • Yep, page source contains a regular space but I printed out (int)charAt(), and saw that the "space" was 160 in "test" and 32 in "ref". It gave me "true". You may want to do something similar to find what that space character actually is: -- something like <% char c = ((String)pageContext.getAttribute("test")).charAt(3); out.println((int)c); %> – Pradyumna Jan 13 '15 at 08:42
  • If I use `` nothing is replaced so no I don't think it's a non breaking space (I have also tried with   and  ) – alain.janinm Jan 13 '15 at 09:27
  • :) " " is decoded to char 32 by the browser. JSTL will treat   as the literal characters " ", not the character represented by char code 32. Scriptlets, as I showed above, give you the flexibility to experiment with raw char codes. good luck.. bye :) – Pradyumna Jan 13 '15 at 11:41