Discussion:
[FreeMarker-user] formating numeric string from variable
lcp
2010-01-12 19:04:57 UTC
Permalink
I am using Freemarker for a content.ftl file with Geoserver to be displayed
in OpenLayers.

I am not able to format my numeric values as strings. I can copy and paste
the examples from the freemarker FAQ page into my script and they do work.
These values are coming from a PostGIS database and are "numeric".

Could someone please tell me what they think I am doing wrong? I gave some
examples of code I have tried.

This is part of the error I get:
Expected method. x?string evaluated instead to
freemarker.template.SimpleScalar on line 17, column 3 in content.ftl. The
problematic instruction: ---------- ==> ${x?string("0")} [on line 17, column
1 in content.ftl] ---------- Java backtrace for programmers: ----------
freemarker.template.TemplateException: Expected method. x?string evaluated
instead to freemarker.template.SimpleScalar on line 17, column 3 in
content.ftl. at...

Thank you, LP

DOESN'T WORK
<#if attribute.name = "last">
<li>% of people living on less than $1 per day PPP - most recent
measurement of this indicator between 2002 and
2007:${attribute.value?string.currency}</li>
</#if>
</#list>
-----------------
DOESN'T WORK
${attribute.value?string(",##0.00")}</li>
-----------------
DOESN'T WORK
${attribute.value}</li>
<#assign x = attribute.value>
${x?string("0")}
------------------
DOES WORK

${attribute.value}</li>
<#assign x = attribute.value>
${x?string} <#-- the same as ${x} -->
------------------
DOES WORK

${attribute.value}</li>
<#assign x = 1.234>
${x?string("0")}
-----------------
--
View this message in context: http://n4.nabble.com/formating-numeric-string-from-variable-tp1012376p1012376.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Daniel Dekany
2010-01-13 13:06:42 UTC
Permalink
Post by lcp
I am using Freemarker for a content.ftl file with Geoserver to be displayed
in OpenLayers.
I am not able to format my numeric values as strings. I can copy and paste
the examples from the freemarker FAQ page into my script and they do work.
These values are coming from a PostGIS database and are "numeric".
Could someone please tell me what they think I am doing wrong? I gave some
examples of code I have tried.
Expected method. x?string evaluated instead to
freemarker.template.SimpleScalar on line 17, column 3 in content.ftl. The
problematic instruction: ---------- ==> ${x?string("0")} [on line 17, column
1 in content.ftl] ---------- Java backtrace for programmers: ----------
freemarker.template.TemplateException: Expected method. x?string evaluated
instead to freemarker.template.SimpleScalar on line 17, column 3 in
content.ftl. at...
I know you said they are numbers, but it still looks like a problem
with the type of x. Because, x?string produces a method when x was a
number or a date or a boolean, but not when x is a string; then it
just returns a string that you can't call, hence the above error.
Could you use ?is_number?string in place of ?string.whatever and
?string(whatever) at the failing locations, to see if there are any
"false"-s? If there are only "true"-s, could you try the same with
?is_string?string to see if there are any "true"-s (in which case you
have some multi-typed values... that possibly can cause issues
depending on which type ?string gives the priority (which I don't know
right now...).)
Post by lcp
Thank you, LP
DOESN'T WORK
<#if attribute.name = "last">
<li>% of people living on less than $1 per day PPP - most recent
measurement of this indicator between 2002 and
2007:${attribute.value?string.currency}</li>
</#if>
</#list>
-----------------
DOESN'T WORK
${attribute.value?string(",##0.00")}</li>
-----------------
DOESN'T WORK
${attribute.value}</li>
<#assign x = attribute.value>
${x?string("0")}
------------------
DOES WORK
That's scary... because attribute.value?whatever meant to be
equivalent with x?whatever after <#assign x = attribute.value>
(unless, of course, you have a #local x that hides it). If it
doesn't... well, I don't think such a serious bug could survive for so
long.
Post by lcp
${attribute.value}</li>
<#assign x = attribute.value>
${x?string} <#-- the same as ${x} -->
${attribute.value?string} should work then too...
Post by lcp
------------------
DOES WORK
${attribute.value}</li>
<#assign x = 1.234>
${x?string("0")}
-----------------
... and also ${attribute.value?string("0")} should work then.
--
Best regards,
Daniel Dekany
lcp
2010-03-25 00:07:39 UTC
Permalink
This was the problem - Geoserver apparently sends out numeric variables as
strings. I fixed this with the Number function to convert strings to
numbers.

${attribute.value?number?round}%

Lela
--
View this message in context: http://n4.nabble.com/formating-numeric-string-from-variable-tp1012376p1690014.html
Sent from the freemarker-user mailing list archive at Nabble.com.
JoHages
2012-07-04 17:05:17 UTC
Permalink
Old topic but I thought I would throw in my two cents.
I was receiving the same error messages for "Expecting a number here, found:
...". The solution it seems for me was to put an IF statement to check if
the variable has content.
<#if attribute.value?has_content>
${attribute.value?number?round}
</#if>

--
View this message in context: http://freemarker.624813.n4.nabble.com/formating-numeric-string-from-variable-tp1012376p4654114.html
Sent from the freemarker-user mailing list archive at Nabble.com.
mahendrapathe
2015-08-12 09:39:17 UTC
Permalink
I found that before using the attribute we have to use
<#setting number_format="0.##">
${attribute?number}

it will rounded the attribute with precision with 2.
You can use nos of # to get your precision

Regards
Mahendra Pathe



--
View this message in context: http://freemarker.624813.n4.nabble.com/formating-numeric-string-from-variable-tp1012376p4655541.html
Sent from the freemarker-user mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Loading...