Java开发过程中经常碰到数据类型的问题
2008-03-29 22:23
615 查看
(1)在把int或double转换成BigDecimal时位数就会相应的增长,为了解决这个问题,可以将double获long型通过自写函数round进行四舍五入
后,在转换成String,然后通过new BigDecimal()转换过来
例如:fosum = new BigDecimal(String.&#118alueOf(round(uo1sum.double&#118alue() + uo2sum.double&#118alue(),3)))
(2)将时间转换成字符
java.util.Date date = new java.util.Date(databean.getTyrq().getTime());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
tyrq = sdf.format(date);
(3)将字符串转换成时间Timestamp类型
public java.sql.Timestamp strToTimestamp(String str){
java.sql.Timestamp sdate = null;
if(str!=null){
if(str.trim().length()==8){
str = str.substring(0,4)+"-"+str.substring(4,6)+"-"+str.substring(6,8);
}
try{
DateFormat df = DateFormat.getDateInstance(DateFormat.DEFAULT,java.util.Locale.CHINA);
java.util.Date date = df.parse(str);
sdate = new java.sql.Timestamp(date.getTime());
}catch(Exception e){
e.printStackTrace();
}
}
return sdate;
}
(4)将double型进行四舍五入
public double round(double v,int scale){
if(scale<0){
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).double&#118alue();
}
(5)将int转换成byte[]
public byte[] InttoByteArray(int num){
int temp = num;
byte[] b = new byte[6];
for(int i=b.length;i>-1;i--){
b[i] = new Integer(temp&0xff).byte&#118alue();
temp = temp >> 8;
}
return b;
}
(6)将int转换成byte
int s=0;
byte b = (byte)s;
后,在转换成String,然后通过new BigDecimal()转换过来
例如:fosum = new BigDecimal(String.&#118alueOf(round(uo1sum.double&#118alue() + uo2sum.double&#118alue(),3)))
(2)将时间转换成字符
java.util.Date date = new java.util.Date(databean.getTyrq().getTime());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
tyrq = sdf.format(date);
(3)将字符串转换成时间Timestamp类型
public java.sql.Timestamp strToTimestamp(String str){
java.sql.Timestamp sdate = null;
if(str!=null){
if(str.trim().length()==8){
str = str.substring(0,4)+"-"+str.substring(4,6)+"-"+str.substring(6,8);
}
try{
DateFormat df = DateFormat.getDateInstance(DateFormat.DEFAULT,java.util.Locale.CHINA);
java.util.Date date = df.parse(str);
sdate = new java.sql.Timestamp(date.getTime());
}catch(Exception e){
e.printStackTrace();
}
}
return sdate;
}
(4)将double型进行四舍五入
public double round(double v,int scale){
if(scale<0){
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).double&#118alue();
}
(5)将int转换成byte[]
public byte[] InttoByteArray(int num){
int temp = num;
byte[] b = new byte[6];
for(int i=b.length;i>-1;i--){
b[i] = new Integer(temp&0xff).byte&#118alue();
temp = temp >> 8;
}
return b;
}
(6)将int转换成byte
int s=0;
byte b = (byte)s;
相关文章推荐
- Java开发过程中经常碰到数据类型的问题
- 在开发过程中经常遇到是将数据封装成list还是map的问题
- 同事在开发过程中经常碰到的问题:无法进行WEB服务侦错
- VC开发过程中数据类型不兼容的问题
- 开发中经常碰到的问题
- Android NDK开发中java数据类型与C/C++数据类型的对应关系
- 一直以来我(每个从事linux开发的人)深受“bug”的困扰,好像“bug”不足以描述这种被问题困扰的无奈。因为当在驱动或BSP的开发过程中,所碰到的问题比解决一个bug难得多。 Li
- Android NDK开发中java数据类型与C/C++数据类型的对应关系
- 将java的集合转换为数据库集合类型 解决weblogic调用存储过程的时候,返回值乱码问题
- java学习之旅10--基本数据类型自动转换_强制转型_类型提升问题
- 表达式计算过程中的类型转换,经常被忽略的问题
- VC常用数据类型使用转换详解(时常碰到类型转换问题)
- java数据类型相关的问题
- Java Web开发环境搭建过程中遇到的若干问题
- java开发中经常遇到的问题
- Java JNI开发时常用数据类型与C++中数据类型转换
- 关于数据序列化(5),定制FastJSON序列化(解决Java大Long类型js的Number接收丢失数据的问题,不序列化某些属性)
- 安卓开发过程中空指针的问题Java.lang.NullPointerException
- Java中Byte类型数据在运算中的问题
- Java double数据类型中的0.0和-0.0问题