
在Java中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,至于两个类的对象能表示最大范围不清楚,理论上能够表示无,无限大的数,只要计算机内存足够大。
这两个类都在javamath包中,因此每次必须在开头处引用该包
BigInteger类型的数字范围较 Integer 类型的数字范围要大得多。我们都知道 Integer 是 Int 的包装类,int 的最大值为 231-1,如果要计算更大的数字,使用Integer 数据类型就无法实现了,所以 Java 中提供了BigInteger 类来处理更大的数字。 BigInteger 支持任意精度的整数,也就是说在运算中 BigInteger 类型可以准确地表示任何大小的整数值而不会丢失任何信息
#基本 *** 作
BigInteger的运算都没有对原值进行 *** 作,而是返回一个新的BigInteger对象,1valueOf(parament); 将参数转换为制定的类型
比如 int a=3;
BigInteger b=BigIntegervalueOf(a);//b=3
String s=”12345”;
BigInteger c=BigIntegervalueOf(s);//c=12345
2add(); 大整数相加
BigInteger a=new BigInteger(“23”);
BigInteger b=new BigInteger(“34”);
a add(b);//57
3subtract(); 相减
4multiply(); 相乘
5divide(); 相除取整
6remainder(); 取余
7pow(); apow(b)=a^b
8gcd(); 最大公约数
9abs(); 绝对值
10negate(); 取反数
11mod(); amod(b)=a%b=aremainder(b);
12max(); min();
13compareTo()两者比较
基本常量(3个):
A=BigIntegerONE 1
B=BigIntegerTEN 10
C=BigIntegerZERO 0
//键盘读入
Scanner cin=new Scanner(Systemin);// 读入
BigInteger m=scannerBigInteger();
#进制转换
//类型转换(返回类型如下)
在日常开发中我们经常会碰到小数计算,而小数直接计算的话会出现一些小小的错误,如下:
Systemoutprintln(101 + 202);
理想输出303,实际上输出的是30300000000000002。这是因为不论是float 还是double都是浮点数,而计算机是二进制的,浮点数会失去一定的精确度。有没有不失精度的办法呢?这里就要用到BigDecimal了。
java的float只能用来进行科学计算或工程计算,在大多数的商业计算中,一般采用javamathBigDecimal类来进行精确计算
基本方法如BigInteger,其他用法如下:
#使用BigDecimal实现四舍五入(支持float和double类型)
#保留有效数字
方法/步骤
一, ExcelUtils 工具类(也就是解析EXCEL文件,判断EXCEL的类型以及数据的类型)
import javaioIOException;
import javaioInputStream;
import javamathBigDecimal;
import javatextSimpleDateFormat;
import javautilArrayList;
import javautilDate;
import javautilList;
import orgapachepoihssfusermodelHSSFDateUtil;
import orgapachepoihssfusermodelHSSFWorkbook;
import orgapachepoissusermodelCell;
import orgapachepoissusermodelRow;
import orgapachepoissusermodelSheet;
import orgapachepoissusermodelWorkbook;
import orgapachepoixssfusermodelXSSFWorkbook;
public class ExcelUtils {
private final static String excel2003L = “xls” ; // 2003-版本的excel
private final static String excel2007U = “xlsx” ; // 2007 +版本的excel
/
描述:获取IO流中的数据,组装成List <List <Object>对象
@param in,fileName
@返回
@throws IOException
/
public List <List <Object> getBankListByExcel(InputStream in,String fileName) throws Exception {
列表<List <Object> list = null ;
//创建的Excel工作薄
Workbook work = this getWorkbook(in,fileName);
if (null == work){
抛出新的 异常(“创建Excel工作薄为空!” );
}
Sheet sheet = null ; //页数
行row = null ; //行数
Cell cell = null ; //列数
list = new ArrayList <List <Object >>();
//遍历的Excel中所有的片
for (int i = 0 ; i <workgetNumberOfSheets(); i ++){
sheet = workgetSheetAt(i);
if (sheet == null ){ continue ;}
//遍历当前片中的所有行
for (int j = sheetgetFirstRowNum(); j <= sheetgetLastRowNum(); j ++){
row = sheetgetRow(j);
if (row == null || rowgetFirstCellNum()== j){ continue ;}
//遍历所有的列
列表<Object> li = new ArrayList <Object>();
for (int y = rowgetFirstCellNum(); y <rowgetLastCellNum(); y ++){
cell = rowgetCell(y);
liadd(this getValue(cell));
}
listadd(LI);
}
}
return 单
}
/
描述:根据文件后缀,自适应上传文件的版本
@param inStr,fileName
@返回
@throws异常
/
public Workbook getWorkbook(InputStream inStr,String fileName) throws Exception {
工作簿wb = null ;
String fileType = fileNamesubstring(fileNamelastIndexOf(“。” ));
if (excel2003Lequals(fileType)){
wb = new HSSFWorkbook(inStr); // 2003-
} else if (excel2007Uequals(fileType)){
wb = new XSSFWorkbook(inStr); // 2007 +
} else {
抛出新的 异常(“解析的文件格式有误!” );
}
返回 wb;
}
/
描述:对表格中数值进行格式化
@param单元格
@返回
/
//解决擅长类型问题,获得数值
public String getValue(Cell cell){
String value = “” ;
if (null == cell){
返回 值
}
switch (cellgetCellType()){
//数值型
案例 CellCELL_TYPE_NUMERIC:
if (HSSFDateUtilisCellDateFormatted(cell)){
//如果是date类型则,获取该单元格的日期值
Date date = HSSFDateUtilgetJavaDate(cellgetNumericCellValue());
SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd” );
value = formatformat(date);;
} else { //纯数字
BigDecimal big = new BigDecimal(cellgetNumericCellValue());
value = bigtoString();
//解决12340去掉后面的0
if (null != value &&!“” equals(valuetrim())){
String [] item = valuesplit(“[。]” );
if (1 <itemlength && “0” equals(item [ 1 ])){
value = item [ 0 ];
}
}
}
break;
//字符串类型
案例 CellCELL_TYPE_STRING:
value = cellgetStringCellValue()。toString();
break;
//公式类型
案例 CellCELL_TYPE_FORMULA:
//读公式计算值
value = StringvalueOf(cellgetNumericCellValue());
if (valueequals(“NaN” )){ //如果获取的数据值为非法值,则转换为获取字符串
value = cellgetStringCellValue()。toString();
}
break;
//布尔类型
案例 CellCELL_TYPE_BOOLEAN:
value = “” + cellgetBooleanCellValue();
break;
默认值:
value = cellgetStringCellValue()。toString();
}
if (“null” endsWith(valuetrim())){
value = “” ;
}
返回 值
}
}
二,定义两个实体类,一个是对于的Excel文件,解析它的数据(ExcelBean),另一个是导入数据库表的实体类(人)
ExcelBeanjava
<strong> <span style = “font-size:18px;” > import orgapachepoixssfusermodelXSSFCellStyle;
公共类 ExcelBean 实现 javaioSerializable {
private String headTextName; //列头(标题)名
private String propertyName; //对应字段名
私有 整数列; //合并单元格数
私人 XSSFCellStyle cellStyle;
public ExcelBean(){
}
public ExcelBean(String headTextName,String propertyName){
这个headTextName = headTextName;
这个propertyName = propertyName;
}
public ExcelBean(String headTextName,String propertyName,Integer cols){
super ();
这个headTextName = headTextName;
这个propertyName = propertyName;
这个cols = cols;
}
public String getHeadTextName(){
return headTextName;
}
public void setHeadTextName(String headTextName){
这个headTextName = headTextName;
}
public String getPropertyName(){
return propertyName;
}
public void setPropertyName(String propertyName){
这个propertyName = propertyName;
}
public Integer getCols(){
返回列 ;
}
公共无效 setCols(Integer cols){
这个cols = cols;
}
上市 XSSFCellStyle getCellStyle(){
返回 cellStyle;
}
公共无效 setCellStyle(XSSFCellStyle cellStyle){
这个 cellStyle = cellStyle;
}
} </ span> </ strong>
peoplejava
import javautilDate;
公共课 人
私有 整数id
private String userName;
私人 字符串密码;
私人 整数年龄;
私人 日期;
public Integer getId(){
返回 id
}
public void setId(Integer id){
这个id = id;
}
public String getUserName(){
return userName;
}
public void setUserName(String userName){
这个userName = userName == null ? null :userNametrim();
}
public String getPassword(){
返回 密码
}
public void setPassword(String password){
这个password = password == null ? null :passwordtrim();
}
public Integer getAge(){
回归 年龄
}
public void setAge(Integer age){
这个age = age
}
public Date getDate(){
退货 日期
}
public void setDate(Date date){
这个date = date
}
}
除法运算/的功能是有区别的。如果相除的两个数都是整数,那么结果也是整数,这叫做取整除法。比如,3/2,结果是1。如果其中有一个是浮点数,那么就是浮点除法,结果是浮点数,比如,30/2,结果是15
以上就是关于BigInteger和BigDecimal全部的内容,包括:BigInteger和BigDecimal、poi导入excel、java做除法运算,为什么除不开时也会得到整数呢等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)