public static int compareCanonicalDecimals(String dec1,String dec2){
  if (dec1.equals(dec2)) {
    return 0;
  }
  if (dec1.charAt(0) == '-' && dec2.charAt(0) != '-') {
    return -1;
  }
  if (dec2.charAt(0) == '-' && dec1.charAt(0) != '-') {
    return 1;
  }
  int dotIdx1=dec1.indexOf('.');
  int dotIdx2=dec2.indexOf('.');
  int result=dotIdx1 - dotIdx2;
  if (result == 0) {
    for (int i=0; result == 0 && i < dotIdx1; i++) {
      result=dec1.charAt(i) - dec2.charAt(i);
    }
    int dec1Length=dec1.length();
    int dec2Length=dec2.length();
    int lastIdx=dec1Length <= dec2Length ? dec1Length : dec2Length;
    for (int i=dotIdx1 + 1; result == 0 && i < lastIdx; i++) {
      result=dec1.charAt(i) - dec2.charAt(i);
    }
    if (result == 0) {
      result=dec1Length - dec2Length;
    }
  }
  if (dec1.charAt(0) == '-') {
    result=-result;
  }
  return result;
}

public static int compareCanonicalDecimals ( String dec1 , String dec2 ) { if ( dec1 . equals ( dec2 ) ) { return 0 ; } if ( dec1 . charAt ( 0 ) == '-' && dec2 . charAt ( 0 ) != '-' ) { return - 1 ; } if ( dec2 . charAt ( 0 ) == '-' && dec1 . charAt ( 0 ) != '-' ) { return 1 ; } int dotIdx1 = dec1 . indexOf ( '.' ) ; int dotIdx2 = dec2 . indexOf ( '.' ) ; int result = dotIdx1 - dotIdx2 ; if ( result == 0 ) { for ( int i = 0 ; result == 0 && i < dotIdx1 ; i ++ ) { result = dec1 . charAt ( i ) - dec2 . charAt ( i ) ; } int dec1Length = dec1 . length ( ) ; int dec2Length = dec2 . length ( ) ; int lastIdx = dec1Length <= dec2Length ? dec1Length : dec2Length ; for ( int i = dotIdx1 + 1 ; result == 0 && i < lastIdx ; i ++ ) { result = dec1 . charAt ( i ) - dec2 . charAt ( i ) ; } if ( result == 0 ) { result = dec1Length - dec2Length ; } } if ( dec1 . charAt ( 0 ) == '-' ) { result = - result ; } return result ; }
compares two canonical decimals to eachother .
