【发布时间】:2012-01-31 17:32:19
【问题描述】:
我创建了一个货币格式化程序类。我希望这是一个 util 类,并且可以被其他应用程序使用。
现在我只取一个字符串,而不是我希望它由导入我的currencyUtil.jar的应用程序设置@
public class CurrencyUtil{
public BigDecimal currencyUtil(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
BigDecimal amount = new BigDecimal("123456789.99"); //Instead I want the amount to be set by the application.
ThemeDisplay themeDisplay =
(ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
Locale locale = themeDisplay.getLocale();
NumberFormat canadaFrench = NumberFormat.getCurrencyInstance(Locale.CANADA_FRENCH);
NumberFormat canadaEnglish = NumberFormat.getCurrencyInstance(Locale.CANADA);
BigDecimal amount = new BigDecimal("123456789.99");
DecimalFormatSymbols symbols = ((DecimalFormat) canadaFrench).getDecimalFormatSymbols();
symbols.setGroupingSeparator('.');
((DecimalFormat) canadaFrench).setDecimalFormatSymbols(symbols);
System.out.println(canadaFrench.format(amount));
System.out.println(canadaEnglish.format(amount));
//Need to have a return type which would return the formats
return amount;
}
}
让调用这个 util 类的其他应用成为
import com.mypackage.CurrencyUtil;
...
public int handleCurrency(RenderRequest request, RenderResponse response) {
String billAmount = "123456.99";
CurrencyUtil CU = new currencyUtil();
//Need to call that util class and set this billAmount to BigDecimal amount in util class.
//Then it should return both the formats or the format I call in the application.
System.out.println(canadaEnglish.format(billAmount); //something like this
}
我做了什么改变?
【问题讨论】:
-
如果可以的话,我会让实用程序类成为无状态的,并且您不需要创建实例,并且它的所有方法都是静态的。
标签: java format return currency