首页 / 更多教程 / 金融记数法 普通数字 相互转换
金融记数法 普通数字 相互转换
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了金融记数法 普通数字 相互转换,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7801字,纯文字阅读大概需要12分钟。
内容图文
![金融记数法 普通数字 相互转换](/upload/InfoBanner/zyjiaocheng/1071/ae0ce30fd6b748eab34206acad2a31a6.jpg)
import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.DecimalFormat; import java.text.NumberFormat; public class OlogyCount { /** * 金融记数法转换成普通数字型。 * <br>例如 132,000,123.00 转换后为 132000123.00 * @param data - 要转换的数字字符串 * @return String - 返回转换后的数字字符串 */ public static String finalToNormal(String data) { if(data.trim()==null||data.trim().equals(""))return ""; //String newData = data.replaceAll(",", ""); //since jdk 1.4 String newData = data; int index = newData.indexOf(‘,‘); while(index != -1){ newData = newData.substring(0, index) + newData.substring(index+1); index = newData.indexOf(‘,‘); } /* int pos = newData.lastIndexOf(‘.‘); int len = 0; //小数位数 if (pos != -1) { len = newData.length() - 1 - pos; } try { double d = Double.parseDouble(newData); NumberFormat form = NumberFormat.getInstance(); String mask = "###0"; if (len > 0) { mask = "###0."; for (int i = 0; i < len; i++) { mask = mask + "0"; } } ((DecimalFormat) form).applyPattern(mask); newData = form.format(d); } catch (Exception e) { e.printStackTrace(); } */return newData; } /** * 普通数字型转换成金融记数法。 * <br>例如 132000123.00 转换后为 132,000,123.00 * @param data - 要转换的数字字符串 * @return String - 返回转换后的数字字符串 */publicstatic String normalToFinal(String data) { if(data.trim()==null||data.trim().equals(""))return ""; int pos = data.lastIndexOf(‘.‘); int len = 0; //小数位数if (pos != -1) { len = data.length() - 1 - pos; } try { double d = Double.parseDouble(data); NumberFormat form = NumberFormat.getInstance(); String mask = "#,##0"; if (len > 0) { mask = "#,##0."; for (int i = 0; i < len; i++) { mask = mask + "0"; } } ((DecimalFormat) form).applyPattern(mask); return form.format(d); } catch (Exception e) { e.printStackTrace(); } returnnull; } /** * 普通数字型转换成金融记数法。 * <br>例如 132000123.00 转换后为 132,000,123.00 * <br>小数点保留两位 * @param data - 要转换的数字 * @return String - 返回转换后的数字字符串 */publicstatic String normalToFinal(double data) { try { NumberFormat form = NumberFormat.getInstance(); String mask = "#,##0.00"; ((DecimalFormat) form).applyPattern(mask); return form.format(data); } catch (Exception e) { e.printStackTrace(); } returnnull; } /** * Bean 中普通数字型转换成金融记数法。 * <br>小数点保留两位 * @param obj - 要转换的对象 * @param obj - 不要转换的属性集合 * @return String - 返回转换后的数字字符串对象 */publicstatic Object beanToNoFinal(Object obj,String jH[]) throws Exception{ Class type = obj.getClass(); boolean flag=false; try{ //*********获得BEAN 的BEAN信息 BeanInfo info = Introspector.getBeanInfo(type); //*********通过BEAN的信息获得性质特制集合 PropertyDescriptor[] props = info.getPropertyDescriptors(); //*********循环传递数据for (int i = 0; i < props.length; i++) { flag=false; //*********获得第1个BEAN的每个属性名与GET方法 String name = props[i].getName(); Method getMethod = props[i].getReadMethod(); //**********如果获得的属性名是jH含有的属性名 把标志设置为真! for(int s=0;s<jH.length;s++){ if(name.equals(jH[s].toString())||"class".equals(name) ||"multipartRequestHandler".equals(name)||"servlet".equals(name) ||"servletWrapper".equals(name))flag=true; } if(!flag){ //*********获得第BEAN的 SET方法 Method setMethod = props[i].getWriteMethod(); if(getMethod.invoke(obj,null)==null)setMethod.invoke(obj,new Object[]{"0"}); setMethod.invoke(obj,new Object[]{OlogyCount.normalToFinal(Double.valueOf(getMethod.invoke(obj,null).toString()).doubleValue())}); } } }catch(Exception e){ thrownew Exception("获得对象转换科学计算法出错!"); } return obj; } /** * 金融记数法换成普通数字型转。 * <br>小数点保留两位 * @param obj - 要转换的对象 * @param obj - 不要转换的属性集合 * @return String - 返回转换后的数字字符串对象 */publicstatic Object beanGetNoFinal(Object obj,String jH[]) throws Exception{ Class type = obj.getClass(); boolean flag=false; try{ //*********获得BEAN 的BEAN信息 BeanInfo info = Introspector.getBeanInfo(type); //*********通过BEAN的信息获得性质特制集合 PropertyDescriptor[] props = info.getPropertyDescriptors(); //*********循环传递数据for (int i = 0; i < props.length; i++) { flag=false; //*********获得第1个BEAN的每个属性名与GET方法 String name = props[i].getName(); Method getMethod = props[i].getReadMethod(); //**********如果获得的属性名是jH含有的属性名 把标志设置为真! for(int s=0;s<jH.length;s++){ if(name.equals(jH[s].toString())||"class".equals(name) ||"multipartRequestHandler".equals(name)||"servlet".equals(name) ||"servletWrapper".equals(name))flag=true; } if(!flag){ //*********获得第BEAN的 SET方法 Method setMethod = props[i].getWriteMethod(); if(getMethod.invoke(obj,null)==null)setMethod.invoke(obj,new Object[]{"0"}); setMethod.invoke(obj,new Object[]{new String(OlogyCount.finalToNormal(getMethod.invoke(obj,null).toString().trim()))}); } } }catch(Exception e){ thrownew Exception("获得对象科学计算法转换数字出错!"); } return obj; } /** * Bean 中普通数字型转换成金融记数法。 * <br>小数点保留两位 * @param obj - 要转换的对象 * @param obj - 要转换的属性集合 * @return String - 返回转换后的数字字符串对象 */publicstatic Object beanToFinal(Object obj,String jH[]) throws Exception{ Class type = obj.getClass(); boolean flag=false; try{ //*********获得BEAN 的BEAN信息 BeanInfo info = Introspector.getBeanInfo(type); //*********通过BEAN的信息获得性质特制集合 PropertyDescriptor[] props = info.getPropertyDescriptors(); //*********循环传递数据for (int i = 0; i < props.length; i++) { flag=false; //*********获得第1个BEAN的每个属性名与GET方法 String name = props[i].getName(); Method getMethod = props[i].getReadMethod(); //**********如果获得的属性名是jH含有的属性名 把标志设置为真! for(int s=0;s<jH.length;s++){ if(name.equals(jH[s].toString())&&!"class".equals(name) &&!"multipartRequestHandler".equals(name)&&!"servlet".equals(name) &&!"servletWrapper".equals(name))flag=true; } // if(flag){ //*********获得第BEAN的 SET方法 Method setMethod = props[i].getWriteMethod(); if(getMethod.invoke(obj,null)==null)setMethod.invoke(obj,new Object[]{"0"}); setMethod.invoke(obj,new Object[]{OlogyCount.normalToFinal(Double.valueOf(getMethod.invoke(obj,null).toString()).doubleValue())}); //System.out.println(name+"---"+getMethod.invoke(obj,null).toString()); } } }catch(Exception e){ thrownew Exception("获得对象转换科学计算法出错!"); } return obj; } /** * 金融记数法换成普通数字型转。 * <br>小数点保留两位 * @param obj - 要转换的对象 * @param obj - 要转换的属性集合 * @return String - 返回转换后的数字字符串对象 */publicstatic Object beanGetFinal(Object obj,String jH[]) throws Exception{ Class type = obj.getClass(); boolean flag=false; String name=""; Method getMethod=null; try{ //*********获得BEAN 的BEAN信息 BeanInfo info = Introspector.getBeanInfo(type); //*********通过BEAN的信息获得性质特制集合 PropertyDescriptor[] props = info.getPropertyDescriptors(); //*********循环传递数据for (int i = 0; i < props.length; i++) { flag=false; //*********获得第1个BEAN的每个属性名与GET方法 name = props[i].getName(); getMethod = props[i].getReadMethod(); //**********如果获得的属性名是jH含有的属性名 把标志设置为真! System.out.println(name); for(int s=0;s<jH.length;s++){ if(name.equals(jH[s].toString())&&!"class".equals(name) &&!"multipartRequestHandler".equals(name)&&!"servlet".equals(name) &&!"servletWrapper".equals(name))flag=true; } if(flag){ //*********获得第BEAN的 SET方法 Method setMethod = props[i].getWriteMethod(); if(getMethod.invoke(obj,null)==null)setMethod.invoke(obj,new Object[]{"0"}); if(Double.valueOf(getMethod.invoke(obj,null).toString()).doubleValue()!=new Double("0").doubleValue()) setMethod.invoke(obj,new Object[]{new String(OlogyCount.finalToNormal(getMethod.invoke(obj,null).toString().trim()))}); } } }catch(Exception e){ try { System.out.println(e+e.getMessage()+name+getMethod.invoke(obj,null).toString()); } catch (IllegalArgumentException e1) { // TODO 自动生成 catch 块 e1.printStackTrace(); } catch (IllegalAccessException e1) { // TODO 自动生成 catch 块 e1.printStackTrace(); } catch (InvocationTargetException e1) { // TODO 自动生成 catch 块 e1.printStackTrace(); } thrownew Exception("获得对象科学计算法转换数字出错!"); } return obj; } }
原文:http://www.cnblogs.com/mingforyou/p/4281659.html
内容总结
以上是互联网集市为您收集整理的金融记数法 普通数字 相互转换全部内容,希望文章能够帮你解决金融记数法 普通数字 相互转换所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。