Java in the digital processing

Digital processing:

In solving practical problems, crunching the numbers it is very common, such as math problems, random issues, business currency issues, science and technology issues. Java provides a number of problem-solving classes, such as DecimalFormat class, Math class, Random class, BigInteger class, BigDecimal.

Digital format

Reformat practical problem-solving is very common, such as supermarkets commodity prices, the need to retain two significant figures, Java mainly floating-point data formatting operations, such as by useDecimal class

For no java in digital format follows:
1. If the absolute value data is greater than 0.001 and less than 10000000, Java will be represented in the form of conventional fractional
2.1. If the data is greater than 0.001 or less than the absolute value of 10000000, Java scientific notation will be
required if the format does not solve the above-mentioned practical problems, it need to use theDecimal classTo operate.

For DecimalFormat class special character description

Digitally formatted using application code:
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 class various mathematical operation method

Here only a brief introduction to some of the math methods, we have to grasp through code

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));
	}
}

Another function for rounding method we introduce a separate
public static double ceil (double a) : returns the smallest integer greater than or equal arguments
public static double floor (double a) : returns the greatest integer less than or equal parameters
public static double rint (double a): returns the nearest integer parameter, if two integers and equally close to the same, that is even if
public static int round (float a) : adding the parameter argument to the nearest integer returned after 0.5
public static Long round ( double a): after adding 0.5 parameter returns the nearest integer parameters, and then cast integral growth.

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

Generating a random number within any range

Random number generation, we generally use the random function Math Random methods or classes to implement.
Math.random () generated random number is (including 0 and 1), at between 0 and 1.
So how do we want to have a greater random number, then the process can require a little
== (int) (Math .random () * n) == 0 returns a random number (excluding n) between ~ n-
== m + (int) (Math.random () * n) == returns a random between m ~ m + n number (not including the n + m)

Method Math.random code is as follows:

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));
		
	}
}

It is noteworthy that the results of each output is not necessarily the same.

Random class code implementation
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());
		
	}
}

Well, these are some simple digital processing of the applications, wrote nearly 2 hours before the end of this section to, have dragged on for nearly a week before beginning to write.

Published 63 original articles · won praise 12 · views 4065

Guess you like

Origin blog.csdn.net/qq_45353823/article/details/102159867