This is a very trivial problem for you guys. Please suggest me the way to achieve the optimal solution. My requirement is, I have an incoming Double value, so if the value after the decimal point is 0, than truncate everything after the decimal. For example: 30.0 should become 30, where as 30.12 or 30.1 should stay as it is. Till now I have only figured out a way to know how many digits are there after the decimal point.
package com.convertdatatypes;
public class DoubleCheck {
public static void main(String args[]){
Double value = 30.153;
String val = value.toString();
String[] result = new String[2];
for(int i=0; i<val.length(); i++){
result = val.split("\\.");
System.out.println("Number of Decimals in: " + val + " : " + result[1].length());
}
}
}
use java’s DecimalFormat with the following pattern:
See http://download.oracle.com/javase/1.4.2/docs/api/java/text/DecimalFormat.html