You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Peter Harrison <pe...@nothingbutnet.co.nz> on 2002/10/30 01:25:05 UTC

Number Formatting

Hi Everyone,

New to Velocity...

We have a system which automatically creates the business model - ie business 
object classes - from an XML document.

In velocity we can simply add these objects to the velocity context, and 
display them - which is cool for strings and integers.

However, we are now trying to display floats - which are the reuslts of 
calculations. However the floats are actually money - ie we need to cut the 
precision down to 2dp.

Is there a simple way to format floats in Velocity?

Regards,
Peter

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Number Formatting

Posted by Emyr James <em...@OrbisUK.com>.
You have to be very careful when using NumberFormat to round up money.
This uses ROUND_HALF_EVEN 
(http://java.sun.com/j2se/1.4.1/docs/api/java/math/BigDecimal.html#ROUND_HALF_EVEN) 
rounding which is probably not what you want.
I think it's best to do the rounding calculation explicitly before you use 
the formatter.
I work for a company that writes casino games. The casino games pay out a 
certain fraction of your stake if you win. Because no explicit rounding was 
being done the payout would be a penny out half the time because it was 
relying on DecimalFormat. We fixed it by doing the rounding as an explicit 
operation and then using decimal format to diplay the correctly rounded 2 
decimal place floating point number.

Cheers,
Emyr

According to the docs,
At 16:36 29/10/2002 -0800, you wrote:
>Peter said:
>...
> > Is there a simple way to format floats in Velocity?
>...
>
>the easiest way is probably to just set up a java.text.NumberFormat to your
>liking and drop it in the context as $numberformat or what-have-you.  then
>use it in your template
>
>$numberformat.format( $foo.thisReturnsAFloat )
>
>Nathan Bubna
>nathan@esha.com
>
>
>
>
>--
>To unsubscribe, 
>e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: 
><ma...@jakarta.apache.org>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Number Formatting

Posted by Nathan Bubna <na...@esha.com>.
Peter said:
...
> Is there a simple way to format floats in Velocity?
...

the easiest way is probably to just set up a java.text.NumberFormat to your
liking and drop it in the context as $numberformat or what-have-you.  then
use it in your template

$numberformat.format( $foo.thisReturnsAFloat )

Nathan Bubna
nathan@esha.com




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Problem setting the template

Posted by Daniel Dekany <dd...@freemail.hu>.
Wednesday, November 6, 2002, 6:47:36 AM, K Nataraj wrote:

[snip]
> Now what happens is that on submitting the screen(3rd frameset)
> session is validated and if session is expired then Login screen is
> appearing but in the third frameset. Is it possible for me to
> replace the entire page by the Login screen???? I want to Login
> screen to be displayed in the entire page rather than in frameset.
> Note : My Login.vm file is not a part of frames.
[snip]

Not a Velocity question, but I guess you should print something like
this into the third frame:

...
<p>Login expired. Redirecting to login page...
<script>window.top.location='theURLOfLogin.vm';</script>
<p><i>Click <a href="theURLOfLogin.vm" target="_top">here</a> if
nothing happens.</i>
...


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Problem setting the template

Posted by K Nataraj <kn...@prodapt.com>.
Hello,
     i have a problem setting the template for a particular screen. Iam
using HTML Frames .
In one frameset i have navigation and in the second one i have a directory
tree structure and in the third one i have the actual screen.

 Since i have used session i need to validate on each user action as whether
his session is still alive or not. If alive i proceed with the action
requested else i need to redisplay the Login screen. Now what happens is
that on submitting the screen(3rd frameset) session is validated and if
session is expired then Login screen is appearing but in the third frameset.
Is it possible for me to replace the entire page by the Login screen???? I
want to Login screen to be displayed in the entire page rather than in
frameset. Note  : My Login.vm file is not a part of frames.

Thanks
nataraj



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Number Formatting

Posted by Ken Anderson <ka...@pacific.net>.
I found this pretty helpful for handling $
http://mindprod.com/jglosscurrency.html
Ken Anderson
Pacific.Net


Peter Harrison wrote:
> Hi Everyone,
> 
> New to Velocity...
> 
> We have a system which automatically creates the business model - ie business 
> object classes - from an XML document.
> 
> In velocity we can simply add these objects to the velocity context, and 
> display them - which is cool for strings and integers.
> 
> However, we are now trying to display floats - which are the reuslts of 
> calculations. However the floats are actually money - ie we need to cut the 
> precision down to 2dp.
> 
> Is there a simple way to format floats in Velocity?
> 
> Regards,
> Peter
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
> 
> 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>