金融记数法 普通数字 相互转换

时间:2023-02-17 11:33:08
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 - 返回转换后的数字字符串
*/
public static 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();
}
return null;
}

/**
* 普通数字型转换成金融记数法。
* <br>例如 132000123.00 转换后为 132,000,123.00
* <br>小数点保留两位
*
@param data - 要转换的数字
*
@return String - 返回转换后的数字字符串
*/
public static 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();
}
return null;
}
/**
* Bean 中普通数字型转换成金融记数法。
* <br>小数点保留两位
*
@param obj - 要转换的对象
*
@param obj - 不要转换的属性集合
*
@return String - 返回转换后的数字字符串对象
*/
public static 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){
throw new Exception("获得对象转换科学计算法出错!");
}
return obj;
}
/**
* 金融记数法换成普通数字型转。
* <br>小数点保留两位
*
@param obj - 要转换的对象
*
@param obj - 不要转换的属性集合
*
@return String - 返回转换后的数字字符串对象
*/
public static 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){
throw new Exception("获得对象科学计算法转换数字出错!");
}
return obj;
}
/**
* Bean 中普通数字型转换成金融记数法。
* <br>小数点保留两位
*
@param obj - 要转换的对象
*
@param obj - 要转换的属性集合
*
@return String - 返回转换后的数字字符串对象
*/
public static 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){
throw new Exception("获得对象转换科学计算法出错!");
}
return obj;
}
/**
* 金融记数法换成普通数字型转。
* <br>小数点保留两位
*
@param obj - 要转换的对象
*
@param obj - 要转换的属性集合
*
@return String - 返回转换后的数字字符串对象
*/
public static 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();
}
throw new Exception("获得对象科学计算法转换数字出错!");
}
return obj;
}

}