0

I am having calculation in my java coding, my problem now is whenever i hit 0 divide by 0, it prints the value ?. My question is, can i change the ? to print 0 or something else? Is that possible? Below is my code,

DecimalFormat df = new DecimalFormat ("0.00");
if (rowset[9] == null) {rowset[9]="0";out.print("0.00");} 
else out.print(df.format(Double.valueOf(rowset[9])/Double.valueOf(rowset[10])));
justarandomguy
  • 145
  • 1
  • 13

1 Answers1

1

Maybe something like (not tested):

DecimalFormat df = new DecimalFormat ("0.00");
if (rowset[9] == null) {rowset[9]="0";out.print("0.00");} 
else {
  double result = rowset[9] / rowset[10];
  if( result != result )
     System.out.println( "undefined" );
  else
     out.print( df.format( result ) );
}

After writing this, I did a search and found a better way to test for NAN. I guess I should RTFM. How do you test to see if a double is equal to NaN?

markspace
  • 10,621
  • 3
  • 25
  • 39