Displaying an arbitrary value as currency in the caller's locale is almost

certainly *never* what you want. However, I don't know who's calling these
methods (and they are doing so through Velocity so I can't just recompile
everything to find out; yay for dynamic languages), so I won't remove them
outright. I did remove the dangerous methods from CurrencyUtil.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@1896 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2006-09-05 21:24:11 +00:00
parent 56e58fa431
commit 55910b495d
2 changed files with 37 additions and 21 deletions
+15 -18
View File
@@ -29,8 +29,8 @@ import java.util.Locale;
public class CurrencyUtil public class CurrencyUtil
{ {
/** /**
* Converts a number representing pennies to a currency display string * Converts a number representing pennies to a locale-appropriate currency
* using the supplied local. * display string using the supplied local.
*/ */
public static String currencyPennies (double value, Locale locale) public static String currencyPennies (double value, Locale locale)
{ {
@@ -38,33 +38,30 @@ public class CurrencyUtil
} }
/** /**
* Converts a number representing dollars to a currency display string * Converts a number representing currency in the specified locale to a
* using the supplied locale. * displayable string.
*/ */
public static String currency (double value, Locale locale) public static String currency (double value, Locale locale)
{ {
NumberFormat numberFormatter = return currency(value, NumberFormat.getCurrencyInstance(locale));
NumberFormat.getCurrencyInstance(locale);
return currency(value, numberFormatter);
} }
/** /**
* Converts a number representing pennies to a currency display string * Converts a number representing pennies to a dollars display string using
* using the default local. * the US local.
*/ */
public static String currencyPennies (double value) public static String dollarsPennies (double value)
{ {
return currency(value / 100.0, _defaultFormatter); return currency(value / 100.0, _dollarFormatter);
} }
/** /**
* Converts a number representing dollars to a currency display string * Converts a number representing dollars to a currency display string
* using the default locale. * using the US locale.
*/ */
public static String currency (double value) public static String dollars (double value)
{ {
return currency(value, _defaultFormatter); return currency(value, _dollarFormatter);
} }
/** /**
@@ -76,7 +73,7 @@ public class CurrencyUtil
return nformat.format(value); return nformat.format(value);
} }
/** A number format for the default local. */ /** A number format for formatting dollars. */
protected static NumberFormat _defaultFormatter = protected static NumberFormat _dollarFormatter =
NumberFormat.getCurrencyInstance(); NumberFormat.getCurrencyInstance(Locale.US);
} }
@@ -41,6 +41,24 @@ public class CurrencyTool
/** /**
* Converts a number representing dollars to a currency display string. * 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.dollars(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) public String currency (double value)
{ {
return CurrencyUtil.currency(value, _req.getLocale()); return CurrencyUtil.currency(value, _req.getLocale());
@@ -48,6 +66,7 @@ public class CurrencyTool
/** /**
* Converts a number representing pennies to a currency display string. * 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) public String currencyPennies (double value)
{ {
@@ -55,14 +74,14 @@ public class CurrencyTool
} }
/** /**
* Velocity currently doesn't support floats, so we have to provide * Velocity currently doesn't support floats, so we have to provide our own
* our own support to convert pennies to a dollar amount. * support to convert pennies to a dollar amount.
*/ */
public String penniesToDollars (int pennies) public String penniesToDollars (int pennies)
{ {
return "" + (pennies / 100.0); return "" + (pennies / 100.0);
} }
/** The servlet request we are providing currency functionality for. */ /** The servlet request we are providing currency functionality for. */
protected HttpServletRequest _req; protected HttpServletRequest _req;
} }