//
// $Id$
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2007 Michael Bayne
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.samskivert.velocity;

import java.util.Locale;

import com.samskivert.util.CurrencyUtil;

/**
 * Provides handy currency functions for use in velocity.
 */
public class CurrencyTool
{
    /**
     * Creates a new CurrencyTool which will used the supplied request to
     * look up the locale with which to do currency formatting in.
     */
    public CurrencyTool (Locale locale)
    {
        _locale = locale;
    }

    /**
     * Converts a number representing dollars to a currency display string.
     */
    public String dollars (double value)
    {
        return CurrencyUtil.dollars(value);
    }

    /**
     * Converts a number representing pennies to a displayable dollars value.
     */
    public String dollarsPennies (double value)
    {
        return CurrencyUtil.dollarsPennies(value);
    }

    /**
     * Converts a number representing currency in the requester's locale to a
     * display string. <em>Note:</em> you probably want to be using {@link
     * #dollars}.
     */
    public String currency (double value)
    {
        return CurrencyUtil.currency(value, _locale);
    }

    /**
     * Converts a number representing pennies to a currency display string.
     * <em>Note:</em> you probably want to be using {@link #dollarsPennies}.
     */
    public String currencyPennies (double value)
    {
        return CurrencyUtil.currencyPennies(value, _locale);
    }

    /**
     * Velocity currently doesn't support floats, so we have to provide our own
     * support to convert pennies to a dollar amount.
     */
    public String penniesToDollars (int pennies)
    {
        return "" + (pennies / 100.0);
    }

    /** The locale in which we are providing currency functionality. */
    protected Locale _locale;
}

@@ -1121,23 +1121,16 @@
 quest to
-%0A     *
  look up
@@ -1145,16 +1145,23 @@
 ale with
+%0A     *
  which t
@@ -1761,23 +1761,16 @@
 to a
-%0A     *
  display
  str
@@ -1757,32 +1757,39 @@
 ale to a display
+%0A     *
  string. %3Cem%3ENot
@@ -1832,23 +1832,16 @@
 g %7B@link
-%0A     *
  #dollar
@@ -2044,23 +2044,16 @@
  string.
-%0A     *
  %3Cem%3ENot
@@ -2055,32 +2055,39 @@
 m%3ENote:%3C/em%3E you
+%0A     *
  probably want t
@@ -2340,23 +2340,16 @@
  our own
-%0A     *
  support
@@ -2359,16 +2359,23 @@
  convert
+%0A     *
  pennies
@@ -2470,19 +2470,154 @@
     
-return %22%22 +
+float decimal = (float)(pennies / 100.0);%0A        // if the pennies is a whole number of dollars, then we return just the string number%0A        if
  (pe
@@ -2618,31 +2618,201 @@
 if (pennies 
-/
+%25
  100
-.0
+ == 0) %7B%0A            return String.format(%22%25.0f%22, decimal);%0A        %7D%0A        // otherwise we always return two decimal places%0A        return String.format(%22%25#.2f%22, decimal
 );%0A    %7D%0A%0A  

Fix penniesToDollars().