java中j<num i .length_判断数值有效位数的Java工具类详解

public class AdjustmentDouble{

/**

* @param s 需处理的数据

* @param numOfIntPart 整数位数

* @param numOfDecimalPart 小数位数

* @return 处理过的数据

*/

public static String adjustDouble(String s,int numOfIntPart,int numOfDecimalPart){

//按小数点的位置分割成整数部分和小数部分

String[] array = s.split("\\.");

char[] tempA=new char[numOfIntPart];

char[] tempB=new char[numOfDecimalPart];

//整数部分满足精度要求(情况1)

if(array[0].length()==numOfIntPart){

//直接获取整数部分长度字符

for(int i=0;i

tempA[i]=array[0].charAt(i);

}

//小数部分精度大于或等于指定的精度

if(numOfDecimalPart<=array[1].length()){

for(int i=0;i

tempB[i]=array[1].charAt(i);

}

}

//小数部分精度小于指定的精度

if(numOfDecimalPart>array[1].length()){

for(int i=0;i

if(i

tempB[i]=array[1].charAt(i);

}else{

tempB[i]='0';

}

}

}

if(numOfDecimalPart==0){

return String.valueOf(tempA)+String.valueOf(tempB);

}

return String.valueOf(tempA)+"."+String.valueOf(tempB);

}

//整数部分位数大于精度要求(情况2)

if(array[0].length()>numOfIntPart){

//先倒序获取指定位数的整数

for(int i=array[0].length()-1,j=0;(i>=array[0].length()-numOfIntPart)&&(j

tempA[j]=array[0].charAt(i);

System.out.println(tempA[j]);

}

char[] tempA1=new char[numOfIntPart];

//调整顺序

for(int j=0,k=tempA.length-1;j=0);j++,k--){

tempA1[j]=tempA[k];

System.out.println("tempA1[j]"+tempA1[j]);

}

//小数部分精度大于或等于指定的精度

if(numOfDecimalPart<=array[1].length()){

for(int i=0;i

tempB[i]=array[1].charAt(i);

}

}

//小数部分精度小于指定的精度

if(numOfDecimalPart>array[1].length()){

for(int i=0;i

if(i

tempB[i]=array[1].charAt(i);

}else{

tempB[i]='0';

}

}

}

return String.valueOf(tempA1)+"."+String.valueOf(tempB);

}

//整数部分满足精度要求(情况3)

if(array[0].length()==numOfIntPart){

//直接获取整数部分长度字符

for(int i=0;i

tempA[i]=array[0].charAt(i);

}

//小数部分精度小于指定的精度

if(numOfDecimalPart>array[1].length()){

for(int i=0;i

if(i

tempB[i]=array[1].charAt(i);

}else{

tempB[i]='0';

}

}

}

//小数部分精度大于或等于指定的精度

if(numOfDecimalPart<=array[1].length()){

for(int i=0;i

tempB[i]=array[1].charAt(i);

}

}

if(numOfDecimalPart==0){

return String.valueOf(tempA)+String.valueOf(tempB);

}

return String.valueOf(tempA)+"."+String.valueOf(tempB);

}

//整数部分大于精度要求(情况4)

if(array[0].length()>numOfIntPart){

//先倒序获取指定位数的整数

for(int i=array[0].length()-1,j=0;(i>=array[0].length()-numOfIntPart+1)&&(j

//System.out.println("<<<

tempA[j]=array[0].charAt(i);

}

char[] tempA1=new char[numOfIntPart];

//调整顺序

for(int j=0,k=tempA.length-1;j=0);j++){

tempA1[j]=tempA[k];

k--;

}

//小数部分精度小于指定的精度

if(numOfDecimalPart>array[1].length()){

for(int i=0;i

if(i>=array[1].length()){

tempB[i]='0';

}else{

tempB[i]=array[1].charAt(i);

}

}

}

//小数部分精度大于或等于指定的精度

if(numOfDecimalPart<=array[1].length()){

for(int i=0;i

tempB[i]=array[1].charAt(i);

}

}

if(numOfDecimalPart==0){

return String.valueOf(tempA1)+String.valueOf(tempB);

}

return String.valueOf(tempA1)+"."+String.valueOf(tempB);

}

//整数部分小于精度要求(情况5)

if(array[0].length()

//先倒序获取指定位数的整数

char[] tempA1=new char[numOfIntPart];

for(int i=array[0].length()-1,j=0;(i>=numOfIntPart-array[0].length()-(numOfIntPart-array[0].length()))&&(j

tempA1[j]=array[0].charAt(i);

System.out.println("<<<<<

}

//补0

for(int i=array[0].length();i

tempA1[i]='0';

System.out.println("<<<<<

}

char[] tempA2=new char[numOfIntPart];

//调整顺序

for(int j=0,k=tempA1.length-1;j=0);j++){

tempA2[j]=tempA1[k];

k--;

}

//小数部分精度小于指定的精度

if(numOfDecimalPart>array[1].length()){

for(int i=0;i

if(i

tempB[i]=array[1].charAt(i);

}else{

tempB[i]='0';

}

}

}

//小数部分精度大于或等于指定的精度

if(numOfDecimalPart<=array[1].length()){

for(int i=0;i

tempB[i]=array[1].charAt(i);

}

}

if(numOfDecimalPart==0){

return String.valueOf(tempA2)+String.valueOf(tempB);

}

return String.valueOf(tempA2)+"."+String.valueOf(tempB);

}

//情况(6)

if((array[0].length()

for(int i=0; i< numOfIntPart-array[0].length(); i++){

s = "0"+s ;

}

for(int i = 0 ; i < numOfDecimalPart -array[1].length() ; i++){

s = s+"0" ;

}

return s;

}

return null;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

String s = "1234.123";

s = AdjustmentDouble.adjustDouble(s, 5, 6);

System.out.println(s);

}

}

/** * @project: WebProjectUtil * @class: NumberUtil * @describe: 此工具类用来处理数字方面的逻辑, * 如返回指定位数的随机数字、Double的加减乘除精确运算、指定位数数字用“0”补齐 * @autho: Administrator * @date: 2013-6-7 下午02:26:27 * @alter: Administrator * @alterDate: 2013-6-7 下午02:26:27 * @alterRemark: * @version V1.0 */ public class NumberUtil { private static final int DEF_DIV_SCALE = 2; /** * @return 返回12位随机数 */ public static String randomNumber() { } /** * @param parm * @return 返回指定位数随机数 */ public static String randomNumber(int parm) { } /** * * 两个Double数相加 * * @param v1 * @param v2 * @return Double */ public static Double add(Double v1, Double v2) { } /** * * 两个Double数相减 * * @param v1 * @param v2 * @return Double */ public static Double sub(Double v1, Double v2) { } /** * * 两个Double数相乘 * * @param v1 * @param v2 * @return Double */ public static Double mul(Double v1, Double v2) { } /** * * 两个Double数相除 * * @param v1 * @param v2 * @return Double */ public static Double div(Double v1, Double v2) { } /** * * 两个Double数相除,并保留scale位小数 * * @param v1 * @param v2 * @param scale * @return Double */ public static Double div(Double v1, Double v2, int scale) { } /** * 返回指定Double的负数 * @param v1 * @return */ public static Double neg(Double v1) { /** * @Title: toFixdLengthString * @Description: 将字符串用符号填充位数 * @param str 源字符串 * @param fixdlenth 位数 * @return String * @throws */ public static String toFixdLengthString(String str, int fixdlenth) { } /** * @Title: toFixdLengthString * @Description: 将数字用“0”填充位数 * @param num * @param fixdlenth * @return String * @throws */ public static String toFixdLengthString(int num, int fixdlenth) { } /** * @Title: generateSpaceString * @Description: 得到指定位数占位符 * @param length * @return String * @throws */ public static String generateSpaceString(int length) { } /** * @Title: generateZeroString * @Description: 得到指定位数的“0”的占位符 * @param length * @return String * @throws */ public static String generateZeroString(int length) { } }
把下面这段js代码转换成java代码/** * * @param {array} peopleList 选手列表 * @param {number} round_num 每人打的场数,默认选手人数-1 */ function getTempMatchList(peopleList,round_num) { let p_num = peopleList.length; let partner_group_list = []; //所有有可能搭档的组 let partner_group_list_real = []; //出场的搭档数组 let p_should_round_num = {}; //记录每人上场的次数对象 let ground_num = 0; for (let i = 0; i < peopleList.length; i++) { p_should_round_num[peopleList[i]] = peopleList.length-1; //预先定下前面的搭档池数 } if((p_num*round_num)%4==0){ //整除 ground_num = Math.floor(peopleList.length*round_num/2); for (let i = 0; i < peopleList.length; i++) { const e = peopleList[i]; for (let j = i+1; j < peopleList.length; j++) { const e1 = peopleList[j]; partner_group_list.push([e,e1]); //准确的搭档池 } } partner_group_list_real = [...partner_group_list]; if(round_num>peopleList.length-1){ //必须每人多打几场 let temp_partner_group_list = [...partner_group_list]; let flag = true; while (flag) { //随机拿一组出来 let index = Math.floor(Math.random()*temp_partner_group_list.length); let partner_group_list_real_one = temp_partner_group_list.splice(index,1); let p1 = partner_group_list_real_one[0][0]; let p2 = partner_group_list_real_one[0][1]; if(p_should_round_num[p1]<round_num&&p_should_round_num[p2]<round_num){ partner_group_list_real.push(partner_group_list_real_one[0]); p_should_round_num[p1] +=1; p_should_round_num[p2] +=1; } if(partner_group_list_real.length==ground_num){ flag = false; } } } return partner_group_list_real; }else{ return []; } }
最新发布
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值