Quantcast

formating numeric string from variable

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

formating numeric string from variable

lcp-2
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")}
-----------------
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: formating numeric string from variable

Daniel Dekany
Tuesday, January 12, 2010, 8:04:57 PM, lcp wrote:

> 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...

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...).)

> 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.

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

${attribute.value?string} should work then too...

> ------------------
> 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


------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: formating numeric string from variable

lcp-2
This post was updated on .
Thanks, these "numbers" were actually strings - Geoserver apparently sends out numeric variables as strings. I fixed this with the Number function to convert strings to numbers.

${attribute.value?number?round}%

I have a new resulting problem now -

There are some areas of my map that return blank data, but are actually strings as shown by the suggested test above. These cause no errors when returning "number strings that have been converted to real numbers with the Number function. After using the above Number function, these blank strings cause this error:

"Error: on line 80, column 13 in content.ftl Expecting a number here, found: The problematic instruction: ---------- ==> ${attribute.value?number?round} "

Since these blank strings aren't really missing values (the missing values test ?? on attribute.value says these blank strings aren't missing) I can't use the missing values fix of (attribute.value)!

The Number function is perfect for most of the data since it formatted the string "numbers" nicely with commas. Are there any suggestions of how I can handle these blank strings so they don't bomb the script?

LP
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: formating numeric string from variable

JoHages
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>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: formating numeric string from variable

mahendrapathe
In reply to this post by lcp-2
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
Loading...