I have been trying to find a way to make sure the length of the double "value" is not larger than 10. However, this is becoming difficult to program since I am not rounding it to a certain number of decimal places.
For example, 1234567.8912 and 12.345678912 are both larger than 10 digits however, they will have to be rounded to a different number of decimal places. My logic was to find where the decimal point occurs and rounding the double to "10 - number of digits before the decimal place".
I created two different methods and both methods won't seem to work correctly.
if ((Double.toString(value)).length()> 10){
int counter = 0;
double no_of_dec = 1;
double no_of_int = 1;
double new_value = 0;
for (int i = 0; i< (Double.toString(value)).length(); i++){
if ( (Character.toString((Double.toString(value)).charAt(i))).equals(".") ){
counter = 1;
} if (counter == 1){
no_of_dec = no_of_dec * 10;
} else if (counter == 0){
no_of_int = no_of_int * 10;
}
}
no_of_dec = no_of_dec / (no_of_int);
new_value = (double)Math.round(value * 100d/100d);
return Double.toString(new_value);
} else {
return Double.toString(value);
}
if ((Double.toString(value)).length()> 10){
double no_of_dec = 0;
double no_of_int = 0;
double new_value = 0;
for (int i = 0; i< (Double.toString(value)).length(); i++){
while (!(Character.toString((Double.toString(value)).charAt(i))).equals(".")){
no_of_int = no_of_int + 1;
}
}
no_of_dec = (Double.toString(value)).length() - no_of_int;
no_of_dec = no_of_dec * 10;
new_value = (double)Math.round(value * no_of_dec/no_of_dec);
return Double.toString(new_value);
} else {
return Double.toString(value);
}
}