我已經創建了一個貨幣fomatter類。我希望這是一個util類,可以被其他應用程序使用。 現在我只是走一個字符串,而不是我希望它由應用程序導入我的currencyUtil.jar
創建一個Util類
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
}
我做出什麼樣的變化?
我會做的效用如果可以的話,這個類是無狀態的,你不需要創建一個實例,它的所有方法都是靜態的。 – 2012-01-02 11:13:26