Re: Implement customizable number formatter

Ernst Plüss <[email protected]> Fri, 21 Aug 2015 15:19:36 +0200
Newsgroups gmane.comp.web.freemarker.devel
Message-ID <CAJUfqpWTJv2RSsfjnXuuYNy2j6Knzt-t4=xGwXo7MF+s=4nbuw@mail.gmail.com>
--===============5396183590552769520==
Content-Type: multipart/alternative; boundary=001a11c347d28e3c78051dd21ee7

--001a11c347d28e3c78051dd21ee7
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Hello Daniel

Many thanks for your extensive response!

I didn't ask to work ahaead. I wanted to check whether I could implement
the featrue and create a pull request in reasonable time ;-) .

Actually I'm looking for something different. What I need is a way to make
sure *every* BigDecimal (or whathever type I'm after) is transformed to a
String by user defined code. Since the formatting depends on the value of
the BigDecimal, I cannot use a static pattern.

As of now I registered a "formatBigDecimal()" function which does this. But
every now and then a developer forgets to use it and we end up with wrong
output.

I think writing

${x?string("@formatBigDecimal")}

instead of

formatBigDecimal(${value})

doesn't bring any benefit in my situation.

Regards
Ernst



2015-08-21 14:55 GMT+02:00 Daniel Dekany <[email protected]>:

> I haven't started working on that yet. I *hope* I can start it
> tomorrow or so. So I'm not yet 100% sure how it will look. I only know
> for sure that it must be finished during the next week. (: So I
> recommend you to just quickly shove in your NumberFormat class where
> FreeMarker creates the DecimalFormat-s, and wait what will happen next
> week... You can't really work ahead.
>
> To answer your question, Environmen.formatNumber(Number) is and
> remains a point where number formatting with the current format goes
> through, of course. The interesting thing is though if how will number
> formatters be represented (not java.text.NumberFormat anymore [*]),
> and what object will create them. The (inherited) logic of FreeMarker
> is that you specify a *string* as the "number_format" setting (which
> can even be changed inside the template, in runtime), and then
> internally that becomes to a cached(!) DecimalFormat object for the
> current Locale. So certainly, there will be some simple interface like
> freemarker.core.NumberFormatter, and then you will need to register a
> freemarker.core.NumberFormatterFactory with a unique name (let's say
> "price"), which then you can refer to like
> cfg.setNumberFormat("@price"), <#setting numberFormat=3D"@price">,
> ${x?string("@price")}, etc. And so there, the factory has a chance to
> cache. Furthermore, after the number format string was delegated to
> the NumberFormatFactory based on the name after the "@", it will get
> the format string for parsing, so that stuff like "@price 0.00" can be
> implemented. So those are my initial thoughts. (I will review how date
> formatting works, which has a similarly fancy architecture for a
> while, only that's not a public API... yet.)
>
> *: Why not NumberFormat? Two reasons:
>
>    (a) One important goal of pluggable number formatters will be
>        automatic unit printing. In some apps, the template authors
>        aren't supposed to print the units manually. Like ${price},
>        where price is a number, should automatically print "1000 EUR"
>        or such. That requires the formatter to get the
>        TemplateNumberModel instead of just the Number, as it's the
>        TemplateNumberModel that carries the required meta-information.
>
>    (b) Number format is way too bloated if users meant to implement it
>        for some ad-hoc number format.
>
> --
> Thanks,
>  Daniel Dekany
>
>
> Friday, August 21, 2015, 11:20:12 AM, Ernst Pl=C3=BCss wrote:
>
> > Hello
> >
> > From
> >
> http://stackoverflow.com/questions/32111234/how-to-implement-logic-for-bi=
gdecimal-rendering-in-freemarker
> > I learnded, that some kind of plugable number formatter should come soo=
n.
> >
> > Is there alreday any code on github for this?
> >
> > If not, what's the general aproach add this functionality? After
> > having a look at the sources, I think I could built this into
> > Environmen.formatNumber(Number number). Is this the intended way to
> implement this?
> >
> > Regards
> > Ernst
> >
>
>

--001a11c347d28e3c78051dd21ee7
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><div><div>Hello Daniel<br><br></div>Many thanks for y=
our extensive response!<br><br></div>I didn&#39;t ask to work ahaead. I wan=
ted to check whether I could implement the featrue and create a pull reques=
t in reasonable time ;-) .<br><br></div><div>Actually I&#39;m looking for s=
omething different. What I need is a way to make sure *every* BigDecimal (o=
r whathever type I&#39;m after) is transformed to a String by user defined =
code. Since the formatting depends on the value of the BigDecimal, I cannot=
 use a static pattern.<br><br></div><div>As of now I registered a &quot;for=
matBigDecimal()&quot; function which does this. But every now and then a de=
veloper forgets to use it and we end up with wrong output.<br></div><div><b=
r></div><div>I think writing<br><br>${x?string(&quot;@formatBigDecimal&quot=
;)}<br><br>instead of<br><br>formatBigDecimal(${value})<br></div><div><br><=
/div><div>doesn&#39;t bring any benefit in my situation.<br><br></div><div>=
Regards<br></div><div>Ernst<br></div><div><br></div><br></div><div class=3D=
"gmail_extra"><br><div class=3D"gmail_quote">2015-08-21 14:55 GMT+02:00 Dan=
iel Dekany <span dir=3D"ltr">&lt;<a href=3D"mailto:[email protected]" tar=
get=3D"_blank">[email protected]</a>&gt;</span>:<br><blockquote class=3D"=
gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-=
left:1ex">I haven&#39;t started working on that yet. I *hope* I can start i=
t<br>
tomorrow or so. So I&#39;m not yet 100% sure how it will look. I only know<=
br>
for sure that it must be finished during the next week. (: So I<br>
recommend you to just quickly shove in your NumberFormat class where<br>
FreeMarker creates the DecimalFormat-s, and wait what will happen next<br>
week... You can&#39;t really work ahead.<br>
<br>
To answer your question, Environmen.formatNumber(Number) is and<br>
remains a point where number formatting with the current format goes<br>
through, of course. The interesting thing is though if how will number<br>
formatters be represented (not java.text.NumberFormat anymore [*]),<br>
and what object will create them. The (inherited) logic of FreeMarker<br>
is that you specify a *string* as the &quot;number_format&quot; setting (wh=
ich<br>
can even be changed inside the template, in runtime), and then<br>
internally that becomes to a cached(!) DecimalFormat object for the<br>
current Locale. So certainly, there will be some simple interface like<br>
freemarker.core.NumberFormatter, and then you will need to register a<br>
freemarker.core.NumberFormatterFactory with a unique name (let&#39;s say<br=
>
&quot;price&quot;), which then you can refer to like<br>
cfg.setNumberFormat(&quot;@price&quot;), &lt;#setting numberFormat=3D&quot;=
@price&quot;&gt;,<br>
${x?string(&quot;@price&quot;)}, etc. And so there, the factory has a chanc=
e to<br>
cache. Furthermore, after the number format string was delegated to<br>
the NumberFormatFactory based on the name after the &quot;@&quot;, it will =
get<br>
the format string for parsing, so that stuff like &quot;@price 0.00&quot; c=
an be<br>
implemented. So those are my initial thoughts. (I will review how date<br>
formatting works, which has a similarly fancy architecture for a<br>
while, only that&#39;s not a public API... yet.)<br>
<br>
*: Why not NumberFormat? Two reasons:<br>
<br>
=C2=A0 =C2=A0(a) One important goal of pluggable number formatters will be<=
br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0automatic unit printing. In some apps, the templ=
ate authors<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0aren&#39;t supposed to print the units manually.=
 Like ${price},<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0where price is a number, should automatically pr=
int &quot;1000 EUR&quot;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0or such. That requires the formatter to get the<=
br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0TemplateNumberModel instead of just the Number, =
as it&#39;s the<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0TemplateNumberModel that carries the required me=
ta-information.<br>
<br>
=C2=A0 =C2=A0(b) Number format is way too bloated if users meant to impleme=
nt it<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0for some ad-hoc number format.<br>
<span class=3D"HOEnZb"><font color=3D"#888888"><br>
--<br>
Thanks,<br>
=C2=A0Daniel Dekany<br>
</font></span><div class=3D"HOEnZb"><div class=3D"h5"><br>
<br>
Friday, August 21, 2015, 11:20:12 AM, Ernst Pl=C3=BCss wrote:<br>
<br>
&gt; Hello<br>
&gt;<br>
&gt; From<br>
&gt; <a href=3D"http://stackoverflow.com/questions/32111234/how-to-implemen=
t-logic-for-bigdecimal-rendering-in-freemarker" rel=3D"noreferrer" target=
=3D"_blank">http://stackoverflow.com/questions/32111234/how-to-implement-lo=
gic-for-bigdecimal-rendering-in-freemarker</a><br>
&gt; I learnded, that some kind of plugable number formatter should come so=
on.<br>
&gt;<br>
&gt; Is there alreday any code on github for this?<br>
&gt;<br>
&gt; If not, what&#39;s the general aproach add this functionality? After<b=
r>
&gt; having a look at the sources, I think I could built this into<br>
&gt; Environmen.formatNumber(Number number). Is this the intended way to im=
plement this?<br>
&gt;<br>
&gt; Regards<br>
&gt; Ernst<br>
&gt;<br>
<br>
</div></div></blockquote></div><br></div>

--001a11c347d28e3c78051dd21ee7--


--===============5396183590552769520==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------

--===============5396183590552769520==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
FreeMarker-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-devel

--===============5396183590552769520==--