java – 创建Util类
发布时间:2020-12-15 02:30:24 所属栏目:Java 来源:网络整理
导读:我创建了一个货币fomatter类.我希望它是一个util类,可以被其他应用程序使用. 现在我只是取一个字符串,而不是我希望它由导入我的currencyUtil.jar的应用程序设置 public class CurrencyUtil{ public BigDecimal currencyUtil(RenderRequest renderRequest,Ren
我创建了一个货币fomatter类.我希望它是一个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 } 我做了什么改变? 解决方法
您需要创建CurrencyUtil类的对象.
CurrencyUtil CU = new CurrencyUtil(); //To call a method,BigDecimal value=CU.currencyUtil(request,response); 我建议currenyUtil方法应该是静态的,它需要三个参数. public class CurrencyUtil { public static BigDecimal currencyUtil( RenderRequest renderRequest,RenderResponse renderResponse,String amountStr) throws IOException,PortletException { BigDecimal amount = new BigDecimal(amountStr); ... } } 你可以打电话给, BigDecimal value=CurrencyUtil.currencyUtil(request,response,billAmount); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |