在这里,我们将给大家分享关于Java基础--BigIntegerBigDecimai大数的知识,让您更了解java大数运算biginteger的本质,同时也会涉及到如何更有效地14-03javaBig
在这里,我们将给大家分享关于Java 基础 -- BigInteger BigDecimai大数的知识,让您更了解java大数运算biginteger的本质,同时也会涉及到如何更有效地14-03 java BigInteger类,BigDecimal类,Date类,DateFormat类,Calendar类、17_java之Integer|System|Arrays|Math|BigInteger|BigDecimal、3.2. 数学类(Math、BigInteger、BigDecimal)、ACM中java中BigInteger和Decimal用到的主要函数的内容。
本文目录一览:- Java 基础 -- BigInteger BigDecimai大数(java大数运算biginteger)
- 14-03 java BigInteger类,BigDecimal类,Date类,DateFormat类,Calendar类
- 17_java之Integer|System|Arrays|Math|BigInteger|BigDecimal
- 3.2. 数学类(Math、BigInteger、BigDecimal)
- ACM中java中BigInteger和Decimal用到的主要函数
Java 基础 -- BigInteger BigDecimai大数(java大数运算biginteger)
BigInteger
加减乘除
BigInteger bi1 = new BigInteger("123456789") ; // 声明BigInteger对象
BigInteger bi2 = new BigInteger("987654321") ; // 声明BigInteger对象
System.out.println("加法操作:" + bi2.add(bi1)) ; // 加法操作
System.out.println("减法操作:" + bi2.subtract(bi1)) ; // 减法操作
System.out.println("乘法操作:" + bi2.multiply(bi1)) ; // 乘法操作
System.out.println("除法操作:" + bi2.divide(bi1)) ; // 除法操作
System.out.println("最大数:" + bi2.max(bi1)) ; // 求出最大数
System.out.println("最小数:" + bi2.min(bi1)) ; // 求出最小数
BigInteger result[] = bi2.divideAndRemainder(bi1) ; // 求出余数的除法操作
System.out.println("商是:" + result[0] +
";余数是:" + result[1]) ;
比较大小
Scanner cin = new Scanner (System.in);
BigInteger a,b,c,d;
a=cin.nextBigInteger();
b=cin.nextBigInteger();
c=cin.nextBigInteger();
d=cin.nextBigInteger();
if(a.compareTo(b)<0)//此时由于a<b,则返回值小于0;
{
System.out.println("a<b");
}
if(b.equals(c))//此时由于b==c,则返回值等于0;
{
System.out.println("b==c");
}
if(d.compareTo(c)>0)//此时由于d>c,则返回值大于0;
{
System.out.println("d>c");
}
BigDecimal
加减乘除
14-03 java BigInteger类,BigDecimal类,Date类,DateFormat类,Calendar类
BigInteger类
发
<span>/*<span>
-
<span>BigInteger:可以让超过Integer范围内的数据进行运算
-
构造方法:
-
BigInteger(String val)
<span>*/
<span>public <span>class<span> BigIntegerDemo {
<span>public <span>static <span>void<span> main(String[] args) {
<span>//<span> 这几个测试,是为了简单超过int范围内,Integer就不能再表示,所以就更谈不上计算了。
<span>//<span> Integer i = new Integer(100);
<span>//<span> System.out.println(i);
<span>// <span>//<span> System.out.println(Integer.MAX_VALUE);
<span>//<span> Integer ii = new Integer("2147483647");
<span>//<span> System.out.println(ii);
<span>// <span>//<span> NumberFormatException
<span>//<span> Integer iii = new Integer("2147483648");
<span>//<span> System.out.println(iii);</span><span>//</span><span> 通过大整数来创建对象</span> BigInteger bi = <span>new</span> BigInteger("2147483648"<span>); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(</span>"bi:" +<span> bi);
}
}
BigInteger的运算方法
<span>import<span> java.math.BigInteger;
<span>/*<span>
-
public BigInteger add(BigInteger val):加
-
public BigInteger subtract(BigInteger val):减
-
public BigInteger multiply(BigInteger val):乘
-
public BigInteger divide(BigInteger val):除
-
public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组
<span>*/
<span>public <span>class<span> BigIntegerDemo {
<span>public <span>static <span>void<span> main(String[] args) {
BigInteger bi1 = <span>new BigInteger("100"<span>);
BigInteger bi2 = <span>new BigInteger("50"<span>);</span><span>//</span><span> public BigInteger add(BigInteger val):加</span> Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("add:" +<span> bi1.add(bi2)); </span><span>//</span><span> public BigInteger subtract(BigInteger val):减</span> Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("subtract:" +<span> bi1.subtract(bi2)); </span><span>//</span><span> public BigInteger multiply(BigInteger val):乘</span> Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("multiply:" +<span> bi1.multiply(bi2)); </span><span>//</span><span> public BigInteger divide(BigInteger val):除</span> Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println("divide:" +<span> bi1.divide(bi2)); </span><span>//</span><span> public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组</span> BigInteger[] bis =<span> bi1.divideAndRemainder(bi2); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(</span>"商:" + bis[0<span>]); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(</span>"余数:" + bis[1<span>]);
}
}
BigDecimal类
不可变的、任意精度的有符号十进制数
<span>/*<span>
-
看程序写结果:结果和我们想的有一点点不一样,这是因为float类型的数据存储和整数不一样导致的。它们大部分的时候,都是带有有效数字位。
-
<span>由于在运算的时候,float类型和double很容易丢失精度,演示案例。所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal
-
BigDecimal类:不可变的、任意精度的有符号十进制数,可以解决数据丢失问题。
<span>/
<span>public <span>class<span> BigDecimalDemo {
<span>public <span>static <span>void<span> main(String[] args) {
System.out.println(0.09 + 0.01<span>);
System.out.println(1.0 - 0.32<span>);
System.out.println(1.015 100<span>);
System.out.println(1.301 / 100<span>);Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(</span>1.0 - 0.12<span>);
}
}
BigDecimal的运算
<span>import<span> java.math.BigDecimal;
<span>/*<span>
-
构造方法:
-
public BigDecimal(String val)
-
public BigDecimal add(BigDecimal augend)加
-
public BigDecimal subtract(BigDecimal subtrahend)减
-
public BigDecimal multiply(BigDecimal multiplicand)乘
-
public BigDecimal divide(BigDecimal divisor)除
-
public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):商,几位小数,如何舍取
<span>/
<span>public <span>class<span> BigDecimalDemo {
<span>public <span>static <span>void<span> main(String[] args) {
<span>//<span> System.out.println(0.09 + 0.01);
<span>//<span> System.out.println(1.0 - 0.32);
<span>//<span> System.out.println(1.015 100);
<span>//<span> System.out.println(1.301 / 100);
<span>
BigDecimal bd1 = <span>new BigDecimal("0.09"<span>);
BigDecimal bd2 = <span>new BigDecimal("0.01"<span>);
System.out.println("add:" +<span> bd1.add(bd2));
System.out.println("-------------------"<span>);BigDecimal bd3 </span>= <span>new</span> BigDecimal("1.0"<span>); BigDecimal bd4 </span>= <span>new</span> BigDecimal("0.32"<span>); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(</span>"subtract:" +<span> bd3.subtract(bd4)); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(</span>"-------------------"<span>); BigDecimal bd5 </span>= <span>new</span> BigDecimal("1.015"<span>); BigDecimal bd6 </span>= <span>new</span> BigDecimal("100"<span>); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(</span>"multiply:" +<span> bd5.multiply(bd6)); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(</span>"-------------------"<span>); BigDecimal bd7 </span>= <span>new</span> BigDecimal("1.<a href="https://www.jb51.cc/tag/301/" target="_blank">301</a>"<span>); BigDecimal bd8 </span>= <span>new</span> BigDecimal("100"<span>); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(</span>"divide:" +<span> bd7.divide(bd8)); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(</span>"divide:" + bd7.divide(bd8,3<span>,BigDecimal.ROUND_HALF_UP)); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(</span>"divide:" + bd7.divide(bd8,8<span>,BigDecimal.ROUND_HALF_UP));
}
}
Date类
Date概述
<span>import<span> java.util.Date;
<span>/*<span>
-
Date:表示特定的瞬间,精确到毫秒。
-
构造方法:
-
Date():根据当前的默认毫秒值创建日期对象
-
Date(long date):根据给定的毫秒值创建日期对象
<span>*/
<span>public <span>class<span> DateDemo {
<span>public <span>static <span>void<span> main(String[] args) {
<span>//<span> 创建对象
Date d = <span>new<span> Date();
System.out.println("d:" +<span> d);</span><span>//</span><span> 创建对象 </span><span>//</span><span> long time = Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.currentTimeMillis();</span> <span>long</span> time = 1000 * 60 * 60; <span>//</span><span> 1小时</span> Date d2 = <span>new</span><span> Date(time); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(</span>"d2:" +<span> d2);
}
}
日期和毫秒值的相互转换
<span>import<span> java.util.Date;
<span>/*<span>
-
public long getTime():获取时间,以毫秒为单位
-
public void setTime(long time):设置时间
-
从Date得到一个毫秒值
-
getTime()
-
把一个毫秒值转换为Date
-
构造方法
-
setTime(long time)
<span>*/
<span>public <span>class<span> DateDemo {
<span>public <span>static <span>void<span> main(String[] args) {
<span>//<span> 创建对象
Date d = <span>new<span> Date();</span><span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>时间</span> <span>long</span> time =<span> d.getTime(); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(time); </span><span>//</span><span> Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.currentTimeMillis());</span>
<span>
System.out.println("d:" +<span> d);
<span>//<span> 设置时间
d.setTime(1000<span>);
System.out.println("d:" +<span> d);
}
}
DateFormat
是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。
Date
String -- Date(解析)
<span>import<span> java.text.ParseException;
<span>import<span> java.text.SimpleDateFormat;
<span>import<span> java.util.Date;
<span>/*<span>
-
Date -- String(格式化)
-
public final String format(Date date)
-
String -- Date(解析)
-
public Date parse(String source)
-
DateForamt:可以进行日期和字符串的格式化和解析,但是由于是抽象类,所以使用具体子类SimpleDateFormat。
-
SimpleDateFormat的构造方法:
-
SimpleDateFormat():默认模式
-
SimpleDateFormat(String pattern):给定的模式
-
这个模式字符串该如何写呢?
-
通过查看API,我们就找到了对应的模式
-
年 y
-
月 M
-
日 d
-
时 H
-
分 m
-
秒 s
-
2014年12月12日 12:12:12
<span>*/
<span>public <span>class<span> DateFormatDemo {
<span>public <span>static <span>void main(String[] args) <span>throws<span> ParseException {
<span>//<span> Date -- String
<span>//<span> 创建日期对象
Date d = <span>new<span> Date();
<span>//<span> 创建格式化对象
<span>//<span> SimpleDateFormat sdf = new SimpleDateFormat();
<span>//<span> 给定模式
SimpleDateFormat sdf = <span>new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"<span>);
<span>//<span> public final String format(Date date)
String s =<span> sdf.format(d);
System.out.println(s);</span><span>//</span><span>String -- Date</span> String str = "2008-08-08 12:12:12"<span>; </span><span>//</span><span>在把<a href="https://www.jb51.cc/tag/yige/" target="_blank">一个</a>字符串解析为日期的时候,请注意格式必须和给定的字符串格式匹配</span> SimpleDateFormat sdf2 = <span>new</span> SimpleDateFormat("yyyy-MM-dd HH:mm:ss"<span>); Date dd </span>=<span> sdf2.parse(str); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(dd);
}
}
日期工具类
<span>import<span> java.text.ParseException;
<span>import<span> java.text.SimpleDateFormat;
<span>import<span> java.util.Date;
<span>/**<span>
-
这是日期和字符串相互转换的工具类
-
<span>@author<span> 风清扬
<span>*/
<span>public <span>class<span> DateUtil {
<span>private<span> DateUtil() {
}<span>/**<span>
- 这个方法的作用就是把日期转成一个字符串
- <span>@param<span> d
- 被转换的日期对象
- <span>@param<span> format
- 传递过来的要被转换的格式
- <span>@return<span> 格式化后的字符串
<span>*/
<span>public <span>static<span> String datetoString(Date d,String format) {
<span>//<span> SimpleDateFormat sdf = new SimpleDateFormat(format);
<span>//<span> return sdf.format(d);
<span>return <span>new<span> SimpleDateFormat(format).format(d);
}
<span>/**<span>
- 这个方法的作用就是把一个字符串解析成一个日期对象
- <span>@param<span> s
- 被解析的字符串
- <span>@param<span> format
- 传递过来的要被转换的格式
- <span>@return<span> 解析后的日期对象
- <span>@throws<span> ParseException
<span>*/
<span>public <span>static<span> Date stringToDate(String s,String format)
<span>throws<span> ParseException {
<span>return <span>new<span> SimpleDateFormat(format).parse(s);
}
}
<span>import<span> java.text.ParseException;
<span>import<span> java.util.Date;
<span>/*<span>
-
工具类的测试
<span>*/
<span>public <span>class<span> DateUtilDemo {
<span>public <span>static <span>void main(String[] args) <span>throws<span> ParseException {
Date d = <span>new<span> Date();
<span>//<span> yyyy-MM-dd HH:mm:ss
String s = DateUtil.datetoString(d,"yyyy年MM月dd日 HH:mm:ss"<span>);
System.out.println(s);String s2 </span>= DateUtil.dat<a href="https://www.jb51.cc/tag/eto/" target="_blank">eto</a>String(d,"yyyy年MM月dd日"<span>); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(s2); String s3 </span>= DateUtil.dat<a href="https://www.jb51.cc/tag/eto/" target="_blank">eto</a>String(d,"HH:mm:ss"<span>); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(s3); String str </span>= "2014-10-14"<span>; Date dd </span>= DateUtil.stringToDate(str,"yyyy-MM-dd"<span>); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(dd);
}
}
测试来到世上多少天
<span>import<span> java.text.ParseException;
<span>import<span> java.text.SimpleDateFormat;
<span>import<span> java.util.Date;
<span>import<span> java.util.Scanner;
<span>/*<span>
-
算一下你来到这个世界多少天?
-
分析:
-
A:键盘录入你的出生的年月日
-
B:把该字符串转换为一个日期
-
C:通过该日期得到一个毫秒值
-
D:获取当前时间的毫秒值
-
E:用D-C得到一个毫秒值
-
F:把E的毫秒值转换为年
-
/1000/60/60/24
<span>*/
<span>public <span>class<span> MyYearOldDemo {
<span>public <span>static <span>void main(String[] args) <span>throws<span> ParseException {
<span>//<span> 键盘录入你的出生的年月日
Scanner sc = <span>new<span> Scanner(system.in);
System.out.println("请输入你的出生年月日:"<span>);
String line =<span> sc.nextLine();</span><span>//</span><span> 把该<a href="https://www.jb51.cc/tag/zifuchuanzhuanhuan/" target="_blank">字符串转换</a>为<a href="https://www.jb51.cc/tag/yige/" target="_blank">一个</a>日期</span> SimpleDateFormat sdf = <span>new</span> SimpleDateFormat("yyyy-MM-dd"<span>); Date d </span>=<span> sdf.parse(line); </span><span>//</span><span> 通过该日期得到<a href="https://www.jb51.cc/tag/yige/" target="_blank">一个</a>毫秒值</span> <span>long</span> myTime =<span> d.getTime(); </span><span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>当前时<a href="https://www.jb51.cc/tag/jiande/" target="_blank">间的</a>毫秒值</span> <span>long</span> <a href="https://www.jb51.cc/tag/Now/" target="_blank">Now</a>Time =<span> Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.currentTimeMillis(); </span><span>//</span><span> 用D-C得到<a href="https://www.jb51.cc/tag/yige/" target="_blank">一个</a>毫秒值</span> <span>long</span> time = <a href="https://www.jb51.cc/tag/Now/" target="_blank">Now</a>Time -<span> myTime; </span><span>//</span><span> 把E的毫秒值转换为年</span> <span>long</span> day = time / 1000 / 60 / 60 / 24<span>; Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(</span>"你来到这个世界:" + day + "天"<span>);
}
}
Calendar类
<span>import<span> java.util.Calendar;
<span>/*<span>
-
Calendar:它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
-
public int get(int field):返回给定日历字段的值。日历类中的每个日历字段都是静态的成员变量,并且是int类型。
<span>*/
<span>public <span>class<span> CalendarDemo {
<span>public <span>static <span>void<span> main(String[] args) {
<span>//<span> 其日历字段已由当前日期和时间初始化:
Calendar rightNow = Calendar.getInstance(); <span>//<span> 子类对象</span><span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>年</span> <span>int</span> year =<span> right<a href="https://www.jb51.cc/tag/Now/" target="_blank">Now</a>.get(Calendar.YEAR); </span><span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>月</span> <span>int</span> month =<span> right<a href="https://www.jb51.cc/tag/Now/" target="_blank">Now</a>.get(Calendar.MONTH); </span><span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>日</span> <span>int</span> date =<span> right<a href="https://www.jb51.cc/tag/Now/" target="_blank">Now</a>.get(Calendar.DATE); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(year </span>+ "年" + (month + 1) + "月" + date + "日"<span>);
}
}
Clander的add和set方法
<span>import<span> java.util.Calendar;
<span>/*<span>
-
public void add(int field,int amount):根据给定的日历字段和对应的时间,来对当前的日历进行操作。
-
public final void set(int year,int month,int date):设置当前日历的年月日
<span>*/
<span>public <span>class<span> CalendarDemo {
<span>public <span>static <span>void<span> main(String[] args) {
<span>//<span> 获取当前的日历时间
Calendar c =<span> Calendar.getInstance();</span><span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>年</span> <span>int</span> year =<span> c.get(Calendar.YEAR); </span><span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>月</span> <span>int</span> month =<span> c.get(Calendar.MONTH); </span><span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>日</span> <span>int</span> date =<span> c.get(Calendar.DATE); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(year </span>+ "年" + (month + 1) + "月" + date + "日"<span>); </span><span>//</span> <span>//</span><span> 三年前的今天 </span><span>//</span><span> c.add(Calendar.YEAR,-3); </span><span>//</span> <span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>年 </span><span>//</span><span> year = c.get(Calendar.YEAR); </span><span>//</span> <span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>月 </span><span>//</span><span> month = c.get(Calendar.MONTH); </span><span>//</span> <span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>日 </span><span>//</span><span> date = c.get(Calendar.DATE); </span><span>//</span><span> Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(year + "年" + (month + 1) + "月" + date + "日"); </span><span>//</span><span> 5年后的10天前</span> c.add(Calendar.YEAR,5<span>); c.add(Calendar.DATE,</span>-10<span>); </span><span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>年</span> year =<span> c.get(Calendar.YEAR); </span><span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>月</span> month =<span> c.get(Calendar.MONTH); </span><span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>日</span> date =<span> c.get(Calendar.DATE); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(year </span>+ "年" + (month + 1) + "月" + date + "日"<span>); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(</span>"--------------"<span>); c.set(</span>2011,11,11<span>); </span><span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>年</span> year =<span> c.get(Calendar.YEAR); </span><span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>月</span> month =<span> c.get(Calendar.MONTH); </span><span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>日</span> date =<span> c.get(Calendar.DATE); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank">stem</a>.out.println(year </span>+ "年" + (month + 1) + "月" + date + "日"<span>);
}
}
获取任意一年的二月有多少天
<span>import<span> java.util.Calendar;
<span>import<span> java.util.Scanner;
<span>/*<span>
-
获取任意一年的二月有多少天
-
分析:
-
A:键盘录入任意的年份
-
B:设置日历对象的年月日
-
年就是A输入的数据
-
月是2
-
日是1
-
C:把时间往前推一天,就是2月的最后一天
-
D:获取这一天输出即可
<span>*/
<span>public <span>class<span> CalendarTest {
<span>public <span>static <span>void<span> main(String[] args) {
<span>//<span> 键盘录入任意的年份
Scanner sc = <span>new<span> Scanner(system.in);
System.out.println("请输入年份:"<span>);
<span>int year =<span> sc.nextInt();</span><span>//</span><span> 设置日历对象的年月日</span> Calendar c =<span> Calendar.getInstance(); c.set(year,</span>2,1); <span>//</span><span> 其实是这一年的3月1日 </span><span>//</span><span> 把时间往前推一天,就是2月的最后一天</span> c.add(Calendar.DATE,-1<span>); </span><span>//</span><span> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a>这一天<a href="https://www.jb51.cc/tag/shuchu/" target="_blank">输出</a>即可</span>
<span> System.out.println(c.get(Calendar.DATE));
}
}
17_java之Integer|System|Arrays|Math|BigInteger|BigDecimal
01基本数据类型对象包装类概述
*A:基本数据类型对象包装类概述
*a.基本类型包装类的产生
在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的。而程序开发中,我们需要把字符串数据,
根据需求转换成指定的基本数据类型,如年龄需要转换成int类型,考试成绩需要转换成double类型等
*b.八种基本类型对应的包装类
char Character
int Integer
byte Byte
short Short
long Long
float Float
double Double
boolean Boolean
02Integer类parseInt方法
*A:Integer类parseInt方法:
*a:parseInt()
int i = Integer.parseInt("12");
System.out.println(i/2);//6
*b:parseInt(String s, int radix)
/*
* Integer类静态方法parseInt(String s, int radix)
* radix基数,进制
* "110",2 含义 前面的数字是二进制的,但是方法parseInt运行结果都是十进制
* 指定进制的字符串转换为十进制的整数
*/
public static void function_1(){
int i = Integer.parseInt("110", 2);
System.out.println(i);
}
03Integer类int转成字符串
*A:Integer类int转成字符串:
*a:使用+与字符串拼接
int i = 3;
String s = i+"";
System.out.println(s+1);//"31"
*b:toString(int ,int 进制),任意进制整数转成任意进制的字符串 (了解)
String s1 = Integer.toString(5,2);
System.out.println(s1);
04Integer类构造方法
*A:Integer类构造方法
/*
* Integer类构造方法
* Integer (String s)
* 将数字格式的字符串,传递到Integer类的构造方法中
* 创建Integer对象,包装的是一个字符串
* 将构造方法中的字符串,转成基本数据类型,调用方法,非静态的, intValue()
*/
public static void function_3(){
Integer in = new Integer("100");
int i = in.intValue();
System.out.println(--i);//99
}
05Integer类其他方法
*A:Integer类其他方法
/*
* Integer类的3个静态方法
* 做进制的转换
* 十进制转成二进制 toBinarString(int)
* 十进制转成八进制 toOctalString(int)
*十进制转成十六进制 toHexString(int)
* 三个方法,返回值都是以String形式出现
*/
a:十进制转二,八,十六进制
public static void function_1(){
System.out.println(Integer.toBinaryString(99));
System.out.println(Integer.toOctalString(99));
System.out.println(Integer.toHexString(999));
}
b:获取int的最大值和最小值
/*
* Integer类的静态成员变量
* MAX_VALUE * MIN_VALUE
*/
public static void function(){
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
}
06自动装箱和自动拆箱
*A:自动装箱与自动拆箱:
//JDK1.5新特性
//自动装箱,拆箱的 好处: 基本类型和引用类直接运算
//自动装箱:使用Integer.valueOf(整数值)返回一个封装了该整数值的Integer对象
//自动拆箱:使用Integer对象.intValue()返回Integer对象中封装的整数值 public static void function(){
//引用类型 , 引用变量一定指向对象 //自动装箱, 基本数据类型1, 直接变成了对象
Integer in = 1; // Integer in = new Integer(1)
//in 是引用类型,不能和基本类型运算, 自动拆箱,引用类型in,转换基本类型
//in+1 ==> in.inValue()+1 = 2
//in = 2 自动装箱
in = in + 1;
System.out.println(in);
}
07自动装箱和自动拆箱(当心陷阱!!!)
*A:自动装箱与自动拆箱:
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i==j); // false 比较的是地址
System.out.println(i.equals(j)); // true 继承的是Object重写equals,比较的对象的数据值
System.out.println("===================");
Integer a = 500;//Integer integer=Integer.valueOf(500)
//integer=new Integer(500);
Integer b = 500;
System.out.println(a==b);//false
System.out.println(a.equals(b));//true
System.out.println("===================");
//数据在byte(-128~127)范围内,JVM不会从新new对象
Integer aa = 127; // Integer aa = new Integer(127)
Integer bb = 127; // Integer bb = aa;
System.out.println(aa==bb); //true
System.out.println(aa.equals(bb));//true
08System类方法currentTimeMillis
A:System类方法currentTimeMillis():用于计算程序的执行时间 /* * 获取系统当前毫秒值 * static long currentTimeMillis() * 对程序执行时间测试 */
public static void function(){ long start = System.currentTimeMillis();
//当前时间x-1970年1月1日零时零分零秒 for(int i = 0 ; i < 10000; i++){ System.out.println(i); } long end = System.currentTimeMillis();
//当前时间y-1970年1月1日零时零分零秒 System.out.println(end - start);//当前时间y-当前时间x }
09System类方法exit
*A:System类方法exit()方法
/*
* 退出虚拟机,所有程序全停止
* static void exit(0)
*/
public static void function_1(){
while(true){
System.out.println("hello");
System.exit(0);//该方法会在以后的finally代码块中使用(讲到再说)
}
}
10System类方法gc
A:System类方法gc public class Person { public void finalize(){ System.out.println("垃圾收取了"); } }
/*
* JVM在内存中,收取对象的垃圾
* 当没有更多引用指向该对象时,会自动调用垃圾回收机制回收堆中的对象
* 同时调用回收对象所属类的finalize方法()
* static void gc()
*/
public static void function_2(){
new Person();
new Person();
new Person();
new Person();
new Person();
new Person();
new Person();
new Person();
System.gc();
}
11System类方法getProperties
A:System类方法getProperties(了解)
/*
* 获取当前操作系统的属性:例如操作系统名称,
* static Properties getProperties()
*/
public static void function_3(){
System.out.println( System.getProperties() );
}
12System类方法arraycopy
A:System类方法arraycopy:
/*
* System类方法,复制数组
* arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
* Object src, 要复制的源数组
* int srcPos, 数组源的起始索引
* Object dest,复制后的目标数组
* int destPos,目标数组起始索引
* int length, 复制几个
*/
public static void function_4(){
int[] src = {11,22,33,44,55,66};
int[] desc = {77,88,99,0};
System.arraycopy(src, 1, desc, 1, 2);//将src数组的1位置开始(包含1位置)的两个元素,拷贝到desc的1,2位置上
for(int i = 0 ; i < desc.length ; i++){
System.out.println(desc[i]);
}
}
13Math类的方法_1
A:Math类中的方法 /*
static double sqrt(double d)
返回参数的平方根*/public static void function_4(){ double d = Math.sqrt(-2); System.out.println(d);}
/*0
static double pow(double a, double b)
a的b次方*/public static void function_3(){double d = Math.pow(2, 3);System.out.println(d);}
/*
static double floor(double d)
返回小于或者等于参数d的最大整数*/public static void function_2(){double d = Math.floor(1.5);System.out.println(d);}
/*
static double ceil(double d)
返回大于或者等于参数d的最小整数*/public static void function_1(){double d = Math.ceil(5.1);System.out.println(d);}
/*
static int abs(int i)
获取参数的绝对值*/ public static void function(){int i = Math.abs(0);System.out.println(i); }
14Math类的方法_2
A:Math类的方法_2 /*
static double round(doubl d)
获取参数的四舍五入,取整数*/ public static void function_6(){ double d = Math.round(5.4195); System.out.println(d); }
/*
static double random() 返回随机数 0.0-1.0之间
来源,也是Random类*/
public static void function_5(){
for(int i = 0 ; i < 10 ;i++){
double d = Math.random(); System.out.println(d);
} }
15Arrays工具类
A:Arrays工具类: public class ArraysDemo { public static void main(String[] args) {
function_2(); int[] arr = {56,65,11,98,57,43,16,18,100,200};
int[] newArray = test(arr); System.out.println(Arrays.toString(newArray));
}
/*
* 定义方法,接收输入,存储的是10个人考试成绩 * 将最后三个人的成绩,存储到新的数组中,返回新的数组
*/ public static int[] test(int[] arr){
//对数组排序 Arrays.sort(arr);
//将最后三个成绩存储到新的数组中 int[] result = new int[3];
//成绩数组的最后三个元素,复制到新数组中
// System.arraycopy(arr, 0, result, 0, 3); for(int i = 0 ; i < 3 ;i++){ result[i] = arr[i]; } return result; }
/*
* static String toString(数组)
* 将数组变成字符串
*/
public static void function_2(){
int[] arr = {5,1,4,6,8,9,0};
String s = Arrays.toString(arr);
System.out.println(s);
}
/*
* static int binarySearch(数组, 被查找的元素)
* 数组的二分搜索法
* 返回元素在数组中出现的索引
* 元素不存在, 返回的是 (-插入点-1)
*/
public static void function_1(){
int[] arr = {1,4,7,9,11,15,18};
int index = Arrays.binarySearch(arr, 10);
System.out.println(index);
}
/*
* static void sort(数组)
* 对数组升序排列
*/
public static void function(){
int[] arr = {5,1,4,6,8,9,0};
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
16数组复制练习
*A:数组复制练习:
public static void main(String[] args) {
int[] arr = {56,65,11,98,57,43,16,18,100,200};
int[] newArray = test(arr);
System.out.println(Arrays.toString(newArray));
}
/*
* 定义方法,接收输入,存储的是10个人考试成绩
* 将最后三个人的成绩,存储到新的数组中,返回新的数组
*/
public static int[] test(int[] arr){
//对数组排序 Arrays.sort(arr);
//将最后三个成绩存储到新的数组中 int[] result = new int[3];
//成绩数组的最后三个元素,复制到新数组中
//System.arraycopy(arr, 0, result, 0, 3); for(int i = 0 ; i < 3 ;i++){ result[i] = arr[i]; } return result; }
17BigInteger类概述和构造方法
A:BigInteger类概述和构造方法
public static void main(String[] args) { function(); }
/*
*BigInteger类的构造方法
*传递字符串,要求数字格式,没有长度限制
**/
public static void function(){
BigInteger b = new BigInteger("8465846668464684562385634168451684568645684564564");
System.out.println(b);
BigInteger b1 = new BigInteger("5861694569514568465846668464684562385634168451684568645684564564");
System.out.println(b1);
}
18BigInteger类四则运算
A:BigInteger类四则运算 public static void main(String[] args) { function_1(); }
/*
* BigInteger对象的四则运算
* 调用方法计算,计算结果也只能是BigInteger对象
*/
public static void function_1(){
BigInteger b1 = new BigInteger("5665464516451051581613661405146");
BigInteger b2 = new BigInteger("965855861461465516451051581613661405146");
//计算 b1+b2对象的和,调用方法 add
BigInteger bigAdd = b1.add(b2);//965855867126930032902103163227322810292
System.out.println(bigAdd);
//计算b1-b2对象的差,调用方法subtract
BigInteger bigSub = b1.subtract(b2);
System.out.println(bigSub);
//计算b1*b2对象的乘积,调用方法multiply
BigInteger bigMul = b1.multiply(b2);
System.out.println(bigMul);
//计算b2/b1对象商,调用方法divied
BigInteger bigDiv = b2.divide(b1);
System.out.println(bigDiv);
}
19员工案例的子类的编写
A:BigDecimal类概述
/*
* 计算结果,未知
* 原因: 计算机二进制中,表示浮点数不精确造成
* 超级大型的浮点数据,提供高精度的浮点运算, BigDecimal
System.out.println(0.09 + 0.01);//0.09999999999999999
System.out.println(1.0 - 0.32);//0.6799999999999999
System.out.println(1.015 * 100);//101.49999999999999
System.out.println(1.301 / 100);//0.013009999999999999
*/
20BigDecimal类实现加法减法乘法
A:BigDecimal类实现加法减法乘法 /*
BigDecimal实现三则运算
**/
public static void function(){
BigDecimal b1 = new BigDecimal("0.09");
BigDecimal b2 = new BigDecimal("0.01");
//计算b1+b2的和,调用方法addBigDecimal bigAdd = b1.add(b2);System.out.println(bigAdd);
BigDecimal b3 = new BigDecimal("1");
BigDecimal b4 = new BigDecimal("0.32");
//计算b3-b2的差,调用方法subtractBigDecimal
bigSub = b3.subtract(b4);
System.out.println(bigSub);
BigDecimal b5 = new BigDecimal("1.015");
BigDecimal b6 = new BigDecimal("100");
//计算b5*b6的成绩,调用方法
multiplyBigDecimal bigMul = b5.multiply(b6);
System.out.println(bigMul);}
21BigDecimal类实现除法
A:BigDecimal类实现除法
/*
* BigDecimal实现除法运算
* divide(BigDecimal divisor, int scale, int roundingMode)
* int scale : 保留几位小数
* int roundingMode : 保留模式
* 保留模式 阅读API文档
* static int ROUND_UP 向上+1
* static int ROUND_DOWN 直接舍去
* static int ROUND_HALF_UP >= 0.5 向上+1
* static int ROUND_HALF_DOWN > 0.5 向上+1 ,否则直接舍去
*/
public static void function_1(){
BigDecimal b1 = new BigDecimal("1.0301");
BigDecimal b2 = new BigDecimal("100");
//计算b1/b2的商,调用方法divied
BigDecimal bigDiv = b1.divide(b2,2,BigDecimal.ROUND_HALF_UP);//0.01301
System.out.println(bigDiv);
}
3.2. 数学类(Math、BigInteger、BigDecimal)
1. Math类
Math
类提供了一些基本的数学函数,如求平方根、绝对值、三角函数等。它是一个final
类,并且所有的方法都是static
的,因此无需创建对象,直接使用类名调用方法即可。
以下是Math
类的一些常用方法:
abs(double a)
:返回参数的绝对值。ceil(double a)
:返回大于或等于参数的最小整数值。floor(double a)
:返回小于或等于参数的最大整数值。round(double a)
:返回参数四舍五入后的整数值。max(double a, double b)
:返回两个参数中的最大值。min(double a, double b)
:返回两个参数中的最小值。sqrt(double a)
:返回参数的平方根。pow(double a, double b)
:返回a
的b
次幂。sin(double a)
:返回参数的正弦值。cos(double a)
:返回参数的余弦值。tan(double a)
:返回参数的正切值。
2. BigInteger类
BigInteger
类表示任意精度的整数。在处理大整数时,int
和long
的范围可能不够用,此时可以使用BigInteger
类。BigInteger
类提供了大量的方法来操作大整数,如加法、减法、乘法、除法等。
以下是创建BigInteger
对象的一些方法:
BigInteger(String val)
:根据字符串创建BigInteger
对象。valueOf(long val)
:返回一个等于指定long
值的BigInteger
对象。
以下是BigInteger
类的一些常用方法:
add(BigInteger val)
:返回两个BigInteger
对象的和。subtract(BigInteger val)
:返回两个BigInteger
对象的差。multiply(BigInteger val)
:返回两个BigInteger
对象的积。divide(BigInteger val)
:返回两个BigInteger
对象的商。mod(BigInteger val)
:返回两个BigInteger
对象的余数。pow(int exponent)
:返回当前BigInteger
对象的指定次幂。
3. BigDecimal类
BigDecimal
类表示任意精度的小数。在处理需要高精度计算的小数时,float
和double
的范围和精度可能不够用,此时可以使用BigDecimal
类。BigDecimal
类提供了大量的方法来操作小数,如加法、减法、乘法、除法等。
以下是创建BigDecimal
对象的一些方法:
BigDecimal(String val)
:根据字符串创建BigDecimal
对象。valueOf(double val)
:返回一个等于指定double
值的BigDecimal
对象。
以下是BigDecimal
类的一些常用方法:
add(BigDecimal val)
:返回两个BigDecimal
对象的和。subtract(BigDecimal val)
:返回两个BigDecimal
对象的差。multiply(BigDecimal val)
:返回两个BigDecimal
对象的积。divide(BigDecimal val, int scale, RoundingMode roundingMode)
:返回两个BigDecimal
对象的商,保留指定小数位数,并使用指定的舍入模式。setScale(int newScale, RoundingMode roundingMode)
:返回一个BigDecimal
对象,保留指定小数位数,并使用指定的舍入模式。
4. 示例
下面是一个使用Math
、BigInteger
和BigDecimal
类的示例:
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
public class Main {
public static void main(String[] args) {
// 使用Math类
System.out.println("绝对值:" + Math.abs(-10)); // 输出 "绝对值:10"
System.out.println("平方根:" + Math.sqrt(9)); // 输出 "平方根:3.0"
System.out.println("最大值:" + Math.max(3, 7)); // 输出 "最大值:7"
// 使用BigInteger类
BigInteger num1 = new BigInteger("987654321098765432");
BigInteger num2 = new BigInteger("123456789012345678");
System.out.println("大整数相加:" + num1.add(num2)); // 输出 "大整数相加:1111111110111111110"
System.out.println("大整数相减:" + num1.subtract(num2)); // 输出 "大整数相减:864197532086419754"
System.out.println("大整数相乘:" + num1.multiply(num2)); // 输出 "大整数相乘:121932631137021795435340303682"
// 使用BigDecimal类
BigDecimal decimal1 = new BigDecimal("123.456");
BigDecimal decimal2 = new BigDecimal("789.012");
System.out.println("高精度小数相加:" + decimal1.add(decimal2)); // 输出 "高精度小数相加:912.468"
System.out.println("高精度小数相减:" + decimal1.subtract(decimal2)); // 输出 "高精度小数相减:-665.556"
// 高精度小数相乘
BigDecimal decimal3 = decimal1.multiply(decimal2);
System.out.println("高精度小数相乘:" + decimal3); // 输出 "高精度小数相乘:97421.697632"
// 高精度小数相除
BigDecimal decimal4 = decimal1.divide(decimal2, 5, RoundingMode.HALF_UP);
System.out.println("高精度小数相除:" + decimal4); // 输出 "高精度小数相除:0.15649"
// 设置小数位数和舍入模式
BigDecimal decimal5 = decimal3.setScale(2, RoundingMode.HALF_UP);
System.out.println("高精度小数保留2位小数:" + decimal5); // 输出 "高精度小数保留2位小数:97421.70"
}
}
通过这个示例,您可以了解到Math
、BigInteger
和BigDecimal
的基本用法和常用方法。在实际编程过程中,您会经常使用这些类来处理数学计算。希望这个介绍能帮助您更好地学习和理解Java中的数学类。
推荐阅读:
https://mp.weixin.qq.com/s/dV2JzXfgjDdCmWRmE0glDA
https://mp.weixin.qq.com/s/an83QZOWXHqll3SGPYTL5g
ACM中java中BigInteger和Decimal用到的主要函数
java中大数以及高精度常用函数
使用java大数类解决问题时我们需要注意两个方面:1、不能有包名,也就是说我们要把主类放到默认的包里,如果你的代码里出现形如package cn.gov.test;这样的代码你很有可能会收获到RE
2、提交的类的类名必须为Main,如果是其他的名字你有可能收获到CE也有可能收获到WA(例如UVA)
Scanner cin=new Scanner(System.in);// 读入
一.BigInteger
import java.math.BigInteger;
BigInteger.ZERO //大整数0 BigInteger.ONE //大整数1 BigInteger.TEN //大整数10
BigInteger.valueOf(long val)//返回一个BigInteger,其值等于指定long。
public BigInteger(String val) //构造 :讲一个字符串变为BigIngeter类型的数据public BigInteger add(BigInteger val) //普通加法public BigInteger subtract(BigInteger val)//普通减法public BigInteger multiply(BigInteger val)//普通乘法public BigInteger divide(BigInteger val)//普通除法public BigInteger max(BigInteger val)//返回两个数中的最大值public BigInteger min(BigInteger val)//返回两者中的最小值public BigInteger[] divideAndRemainder(BigInteger val)//除法操作,数组的第一个元素作为除法的商,第二个元素作为除法的余数
mod(BigInteger val); //求余
gcd(BigInteger val); //求最大公约数 (以下都简写)
abs();//返回此值的绝对值
and(BigInteger val); //返回 this&val 的BigInteger
andNot(BigInteger val); //返回 this&~val 的BigInteger
bitCount(); //返回BIgInteger的二进制补码形式中与符号不同的位的数量
compareTo(BigInteger val); //将this 与val比较,大于,等于,小于分别返回1,0,-1;
doubleValue(); //将此BigInteger转换为double型返回;
getLowestSetBit();//返回此BigInteger最右端1比特的索引;
hashCode();//返回此BIgInteger的哈希码;
isProbablePrime();//判断是否为素数(true/false);
modInverse(BigInteger m);返回其值为( mod m)的BigInteger;
negate();返回(-this)的BigInteger;
not();// ~this
or(BIgInteger val);// 返回(this | val)
xor(BigInteger val);返回(this^val);
toString(int radix);//返回指定基数的字符串形式
shiftRight(int n);//返回(this>>n)的BigInteger
shiftLeft(int n);//返回值为(thsi<<n)的BigInteger
示例:
public class Main{
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("123456789") ; // 声明BigInteger对象
BigInteger bi2 = new BigInteger("987654321") ; // 声明BigInteger对象
System.out.println("加法操作:" + bi2.add(bi1)) ; // 加法操作
System.out.println("减法操作:" + bi2.subtract(bi1)) ; // 减法操作
System.out.println("乘法操作:" + bi2.multiply(bi1)) ; // 乘法操作
System.out.println("除法操作:" + bi2.divide(bi1)) ; // 除法操作
System.out.println("最大数:" + bi2.max(bi1)) ; // 求出最大数
System.out.println("最小数:" + bi2.min(bi1)) ; // 求出最小数
BigInteger result[] = bi2.divideAndRemainder(bi1) ;//求出余数的除法操作
System.out.println("商是:" + result[0] +
";余数是:" + result[1]) ;
}
}
二.BigDecimal
import java.math.BigDecimal;
public BigDecimal(double val) //构造 将double表示形式转换为BIgDecimalpublic BigDecimal(int val)//同上public BigDecimal(String val)//将字符串表示形式转换为BigDecimalpublic BigDecimal add(BigDecimal augend)//普通加法public BigDecimal subtract(BigDecimal subtrahend)//普通减法public BigDecimal multiply(BigDecimal multiplicand)//普通乘法public BigDecimal divide(BigDecimal divisor)//普通除法
public BigDecimal pow(int n) //返回大数的n次幂
one.compareTo(two); //在数字上比较大小(大于,等于,小于分别返回1,0 -1);
toString() 将BigDecimal对象的数值转换成字符串。
doubleValue() 将BigDecimal对象中的值以双精度数返回。
floatValue() 将BigDecimal对象中的值以单精度数返回。
longValue() 将BigDecimal对象中的值以长整数返回。
intValue() 将BigDecimal对象中的值以整数返回。
示例:
public class Main{
public static void main(String[] args) {
System.out.println("加法运算:" + MyMath.round(MyMath.add(10.345,3.333),1)) ;
System.out.println("减法运算:" + MyMath.round(MyMath.sub(10.345,3.333),3)) ;
System.out.println("乘法运算:" + MyMath.round(MyMath.mul(10.345,3.333),4)) ;
System.out.println("除法运算:" + MyMath.div(10.345,3.333,3)) ;
}
}
BigDecimal.setScale()方法用于格式化小数点
setScale(val)表示保留val位小数,默认用四舍五入方式 setScale(val,BigDecimal.ROUND_DOWN)直接删除多余的小数位,如2.35会变成2.3 setScale(val,BigDecimal.ROUND_UP)进位处理,2.35变成2.4 setScale(val,BigDecimal.ROUND_HALF_UP)四舍五入,2.35变成2.4
setScaler(val,BigDecimal.ROUND_HALF_DOWN)四舍五入,2.35变成2.3,如果是5则向下舍
setScaler(val,BigDecimal.ROUND_CEILING)接近正无穷大的舍入
setScaler(val,BigDecimal.ROUND_FLOOR)接近负无穷大的舍入,数字>0和ROUND_UP作用一样,数字<0和ROUND_DOWN作用一样
setScaler(val,BigDecimal.ROUND_HALF_EVEN)向最接近的数字舍入,如果与两个相邻数字的距离相等,则向相邻的偶数舍入.
原文出处:https://www.cnblogs.com/csushl/p/10326634.html
关于Java 基础 -- BigInteger BigDecimai大数和java大数运算biginteger的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于14-03 java BigInteger类,BigDecimal类,Date类,DateFormat类,Calendar类、17_java之Integer|System|Arrays|Math|BigInteger|BigDecimal、3.2. 数学类(Math、BigInteger、BigDecimal)、ACM中java中BigInteger和Decimal用到的主要函数等相关内容,可以在本站寻找。
本文标签: