常用方法1 ---数学处理类
来源:互联网 发布:mark knopfler知乎 编辑:程序博客网 时间:2024/05/21 13:58
import java.math.BigDecimal;
public class AbMathUtil{
/**
* 四舍五入.
*
* @param number 原数
* @param decimal 保留几位小数
* @return 四舍五入后的值
*/
public static BigDecimal round(double number, int decimal){
return new BigDecimal(number).setScale(decimal, BigDecimal.ROUND_HALF_UP);
}
/**
* 描述:字节数组转换成16进制串.
*
* @param b the b
* @param length the length
* @return the string
*/
public static String byte2HexStr(byte[] b, int length){
String hs = "";
String stmp = "";
for (int n = 0; n < length; ++n) {
stmp = Integer.toHexString(b[n] & 0xFF);
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else {
hs = hs + stmp;
}
hs = hs + ",";
}
return hs.toUpperCase();
}
/**
* 二进制转为十六进制.
*
* @param binary the binary
* @return char hex
*/
public static char binaryToHex(int binary) {
char ch = ' ';
switch (binary){
case 0:
ch = '0';
break;
case 1:
ch = '1';
break;
case 2:
ch = '2';
break;
case 3:
ch = '3';
break;
case 4:
ch = '4';
break;
case 5:
ch = '5';
break;
case 6:
ch = '6';
break;
case 7:
ch = '7';
break;
case 8:
ch = '8';
break;
case 9:
ch = '9';
break;
case 10:
ch = 'a';
break;
case 11:
ch = 'b';
break;
case 12:
ch = 'c';
break;
case 13:
ch = 'd';
break;
case 14:
ch = 'e';
break;
case 15:
ch = 'f';
break;
default:
ch = ' ';
}
return ch;
}
/**
*
* 一维数组转为二维数组
*
*
* @param m the m
* @param width the width
* @param height the height
* @return the int[][]
*/
public static int[][] arrayToMatrix(int[] m, int width, int height) {
int[][] result = new int[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int p = j * height + i;
result[i][j] = m[p];
}
}
return result;
}
/**
*
* 二维数组转为一维数组
*
*
* @param m the m
* @return the double[]
*/
public static double[] matrixToArray(double[][] m) {
int p = m.length * m[0].length;
double[] result = new double[p];
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++) {
int q = j * m.length + i;
result[q] = m[i][j];
}
}
return result;
}
/**
* 描述:int数组转换为double数组.
*
* @param input the input
* @return the double[]
*/
public static double[] intToDoubleArray(int[] input) {
int length = input.length;
double[] output = new double[length];
for (int i = 0; i < length; i++){
output[i] = Double.valueOf(String.valueOf(input[i]));
}
return output;
}
/**
* 描述:int二维数组转换为double二维数组.
*
* @param input the input
* @return the double[][]
*/
public static double[][] intToDoubleMatrix(int[][] input) {
int height = input.length;
int width = input[0].length;
double[][] output = new double[height][width];
for (int i = 0; i < height; i++) {
// 列
for (int j = 0; j < width; j++) {
// 行
output[i][j] = Double.valueOf(String.valueOf(input[i][j]));
}
}
return output;
}
/**
* 计算数组的平均值.
*
* @param pixels 数组
* @return int 平均值
*/
public static int average(int[] pixels) {
float m = 0;
for (int i = 0; i < pixels.length; ++i) {
m += pixels[i];
}
m = m / pixels.length;
return (int) m;
}
/**
* 计算数组的平均值.
*
* @param pixels 数组
* @return int 平均值
*/
public static int average(double[] pixels) {
float m = 0;
for (int i = 0; i < pixels.length; ++i) {
m += pixels[i];
}
m = m / pixels.length;
return (int) m;
}
}
public class AbMathUtil{
/**
* 四舍五入.
*
* @param number 原数
* @param decimal 保留几位小数
* @return 四舍五入后的值
*/
public static BigDecimal round(double number, int decimal){
return new BigDecimal(number).setScale(decimal, BigDecimal.ROUND_HALF_UP);
}
/**
* 描述:字节数组转换成16进制串.
*
* @param b the b
* @param length the length
* @return the string
*/
public static String byte2HexStr(byte[] b, int length){
String hs = "";
String stmp = "";
for (int n = 0; n < length; ++n) {
stmp = Integer.toHexString(b[n] & 0xFF);
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else {
hs = hs + stmp;
}
hs = hs + ",";
}
return hs.toUpperCase();
}
/**
* 二进制转为十六进制.
*
* @param binary the binary
* @return char hex
*/
public static char binaryToHex(int binary) {
char ch = ' ';
switch (binary){
case 0:
ch = '0';
break;
case 1:
ch = '1';
break;
case 2:
ch = '2';
break;
case 3:
ch = '3';
break;
case 4:
ch = '4';
break;
case 5:
ch = '5';
break;
case 6:
ch = '6';
break;
case 7:
ch = '7';
break;
case 8:
ch = '8';
break;
case 9:
ch = '9';
break;
case 10:
ch = 'a';
break;
case 11:
ch = 'b';
break;
case 12:
ch = 'c';
break;
case 13:
ch = 'd';
break;
case 14:
ch = 'e';
break;
case 15:
ch = 'f';
break;
default:
ch = ' ';
}
return ch;
}
/**
*
* 一维数组转为二维数组
*
*
* @param m the m
* @param width the width
* @param height the height
* @return the int[][]
*/
public static int[][] arrayToMatrix(int[] m, int width, int height) {
int[][] result = new int[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int p = j * height + i;
result[i][j] = m[p];
}
}
return result;
}
/**
*
* 二维数组转为一维数组
*
*
* @param m the m
* @return the double[]
*/
public static double[] matrixToArray(double[][] m) {
int p = m.length * m[0].length;
double[] result = new double[p];
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++) {
int q = j * m.length + i;
result[q] = m[i][j];
}
}
return result;
}
/**
* 描述:int数组转换为double数组.
*
* @param input the input
* @return the double[]
*/
public static double[] intToDoubleArray(int[] input) {
int length = input.length;
double[] output = new double[length];
for (int i = 0; i < length; i++){
output[i] = Double.valueOf(String.valueOf(input[i]));
}
return output;
}
/**
* 描述:int二维数组转换为double二维数组.
*
* @param input the input
* @return the double[][]
*/
public static double[][] intToDoubleMatrix(int[][] input) {
int height = input.length;
int width = input[0].length;
double[][] output = new double[height][width];
for (int i = 0; i < height; i++) {
// 列
for (int j = 0; j < width; j++) {
// 行
output[i][j] = Double.valueOf(String.valueOf(input[i][j]));
}
}
return output;
}
/**
* 计算数组的平均值.
*
* @param pixels 数组
* @return int 平均值
*/
public static int average(int[] pixels) {
float m = 0;
for (int i = 0; i < pixels.length; ++i) {
m += pixels[i];
}
m = m / pixels.length;
return (int) m;
}
/**
* 计算数组的平均值.
*
* @param pixels 数组
* @return int 平均值
*/
public static int average(double[] pixels) {
float m = 0;
for (int i = 0; i < pixels.length; ++i) {
m += pixels[i];
}
m = m / pixels.length;
return (int) m;
}
}
0 0
- 常用方法1 ---数学处理类
- JAVA常用数学类方法
- java数学类Math的常用方法
- iOS常用数学方法
- Math数学方法--常用
- java 常用数学方法 Math常用方法 必须掌握--Java的常用类
- java数学函数Math类中常用的方法
- java数学函数Math类中常用的方法
- 常用 数学类宏
- 总结-处理字符串常用方法类
- 常用方法2 ---字符串处理类
- 常用方法3 ---日期处理类
- 常用文件(夹)处理方法工具类
- 常用cookie处理方法工具类
- java数字处理类常用方法汇总
- 常用日期处理方法
- 常用文本处理方法
- NSString 常用处理方法
- LeetCode::Remove Duplicates from Sorted List
- UVa 353 - Pesky Palindromes
- js字符串转换成数字,数字转换成字符串
- makefile学习笔记
- lambda表达式——.net3.5新特性
- 常用方法1 ---数学处理类
- Android学习之路
- AS3的Objec类t与关联数组
- Android LayoutInflater详解
- LeetCode::Remove Duplicates from Sorted List II
- 关于工控系统信息安全的一些思考
- UVA - 196 Spreadsheet
- 从反汇编看C++(一)
- 操作系统课程设计:gdb的使用