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