Re: Change rounding mode
Attila Szegedi <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
I see. I'm afraid FreeMarker can't easily help you with that -- we're
relying on java.text.DecimalFormat, and it doesn't have a configurable
rounding... Daniel's solution would work, though... You could even
extend it to use automatic escaping:
<#function round2 n>
<#if n?is_number><#return ((n*100)?round/100)?
string><#else><#return n></#if>
</#function>
<#escape x as round2(x)>
... rest of your template here ...
</#escape>
And it'd automatically format all numbers emitted from within the
escape block to two decimals.
Attila.
On Oct 15, 2008, at 2:28 PM, David Cuenca wrote:
> I would like to change the formatting that freemarker uses to show a
> Double with only two decimal positions (${x?string("0.##")}). I want
> freemarker to round it with the half-up rule.
>
> Daniel Dekany wrote:
>> Wednesday, October 15, 2008, 9:23:58 AM, David Cuenca wrote:
>>
>>
>>> Hello,
>>>
>>> I would like to know if there is an easy way to change freemarker's
>>> default rounding mode. As the freemarker manual says:
>>>
>>> "Following the financial and statistics practice, the rounding goes
>>> according the so called half-even rule, which means rounding
>>> towards the
>>> nearest ``neighbor'', unless both neighbors are equidistant, in
>>> which
>>> case, it rounds towards the even neighbor..."
>>>
>>> But I would like to use HALF_UP.
>>>
>> Out of curiosity, why?
>>
>> Otherwise I don't think it's possible in FreeMarker, since it's
>> number
>> formatting is what the Java platform provides, and DecimalFormat uses
>> half-even rounding, and it isn't configurable in this regard. That
>> said, you couldn't even do that in a Java program. So, blame the J2SE
>> API guys... ;)
>>
>> Of course, you could write the function for this easily:
>>
>> <#function n3 n>
>> <#return ((n*1000)?round/1000)?string>
>> </#function>
>>
>>
>>> Does anybody know if it's configurable?
>>>
>>> Thanks!
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/