Java中的数字处理

数字处理:

在解决实际问题时,对数字的处理是非常普遍的,如数学问题,随机问题,商业货币问题,科学技术问题。Java就提供了一些解决的问题的类,如DecimalFormat类,Math类,Random类,BigInteger类,BigDecimal.

数字进行格式化

格式化再实际解决问题中非常普遍,如超市的商品价格,需要保留两位有效数字,Java主要对浮点型数据进行格式化操作,如用用到Decimal类

java中对于没有格式化的数字遵循:
1.如果数据绝对值大于0.001并且小于10000000,Java将以常规小数形式表示
2.1.如果数据绝对值大于0.001或者小于10000000,Java将以科学计数法表示
如果上述格式没有解决实际问题的要求,那就需要用到了Decimal类来进行操作。

针对DecimalFormat类中的特殊字符说明

数字格式化使用应用代码:
import java.util.Scanner;
import java.text.DecimalFormat;
public class Main {
	static public void SimgleFormat(String pattern,double value)
	{
		DecimalFormat myFormat=new DecimalFormat(pattern);
		String output=myFormat.format(value);//将数字进行格式化
		System.out.println(value+" "+pattern+" "+output);
	}
	public static void main(String[] args) {
		SimgleFormat("###,###.###",123456.789);
		SimgleFormat("00000000.###kg",123456.789);
		
	}
}

Math类中的各种数学运算方法

这里对于数学运算方法只做一些简要的介绍,通过代码我们来进行掌握

import java.util.Scanner;
import java.text.DecimalFormat;
import java.util.*;
public class Main {
	
	public static void main(String[] args) {
		
		//对于三角函数的理解
		System.out.println("90度的正弦值为:"+Math.sin(Math.PI/2));
		System.out.println("0度的正弦值为:"+Math.cos(0));
		//对于弧度的理解
		System.out.println("120度弧度值为:"+Math.toRadians(120.0));
		System.out.println("π/2的角度值:"+Math.toDegrees(Math.PI/2));
		//对于指数函数方法的理解
		System.out.println("e的平方值"+Math.exp(2));
		System.out.println("2的2次方值:"+Math.pow(2, 2));
		System.out.println("以e为底2的对数"+Math.log(2));
		//对于最大值最小值,绝对值的函数方法
		System.out.println("4和8较大值:"+Math.max(4, 8));
		System.out.println("4.4和4的较小者:"+Math.min(4, 4.4));
		System.out.println("-4的绝对值:"+Math.abs(4));
	}
}

对于另外一种取整函数方法我们单独来介绍
public static double ceil(double a):返回大于等于参数的最小整数
public static double floor(double a):返回小于等于参数的最大整数
public static double rint(double a):返回与参数最接近的整数,如果两个同为整数并且同样接近,则取偶数
public static int round(float a):将参数加上0.5后返回与参数最近的整数
public static long round(double a):将参数加上0.5后返回与参数最近的整数,然后强制转换成长整型。

import java.util.Scanner;
import java.text.DecimalFormat;
import java.util.*;
public class Main {
	
	public static void main(String[] args) {
		
		System.out.println("使用ceil方法取整"+Math.ceil(5.2));
		System.out.println("使用floor方法取整"+Math.floor(2.5));
		System.out.println("使用rint方法取整"+Math.rint(2.7));
		System.out.println("使用round方法取整"+Math.round(2.5f));//加参数0.5后返回最接近的整数
		System.out.println("使用round方法取整"+Math.round(2.5));//加参数0.5后返回最接近的整数,并将结果强制转换成长整型
		
	}
}
运行结果:
使用ceil方法取整6.0
使用floor方法取整2.0
使用rint方法取整3.0
使用round方法取整3
使用round方法取整3

生成任意范围内的随机数

随机数的产生,我们一般用Math函数中的random方法或者Random类来实现。
Math.random()所产生的随机数为在0~1之间(包含0和1),
那么如何我们想要产生更大的随机数,那么就需稍微处理即可
==(int)(Math.random()*n)==返回0~n之间的随机数(不包括n)
==m+(int)(Math.random()*n)==返回m~m+n之间的随机数(不包括n+m)

Math.random方法代码如下:

import java.util.Scanner;
import java.text.DecimalFormat;
import java.util.*;
public class Main {
	public static int GetNum(double m,double n) {
		int s;
		s=(int)m+(int)(Math.random()*(n-m));//生成m~n之间的数
		if(s%2==0)//判断随机数是否为s
		{return s;}//返回s
		else {
			return s+1;//将结果加1后返回
		}
	}
	public static void main(String[] args) {
		
		System.out.println("任意一个2~32之间的偶数:"+GetNum(2,32));
		
	}
}

值得注意的是每次输出的结果都不一定是一样的。

Random类代码实现
import java.util.*;

import java.text.DecimalFormat;
import java.util.*;
public class Main {
	
	public static void main(String[] args) {
		
		Random r=new Random();//实例化一个Random类
		System.out.println("随机产生一个整数"+r.nextInt());
		System.out.println("随机产生一个大于等于0小于10的整数"+r.nextInt(10));
		System.out.println("随机产生一个布尔类的值"+r.nextBoolean());
		System.out.println("随机产生一个浮点值"+r.nextFloat());
		
	}
}

好啦,这些就是对于数字处理的一些简单应用了,写了将近有2个小时才把这一节给结束,拖了有将近一个星期才开始写。

发布了63 篇原创文章 · 获赞 12 · 访问量 4065

猜你喜欢

转载自blog.csdn.net/qq_45353823/article/details/102159867
今日推荐