Java rounding issues -


i using following code round float value given input. cant right. if give $80 should $80.00 , if give $40.009889 should $40.01. how do ?

 public class round { public static float round_this(float num) {     //float num = 2.954165f;     float round = round(num,2);     return round;   }   private static float round(float rval, int rpl) {   float p = (float)math.pow(10,rpl);   rval = rval * p;   float tmp = math.round(rval);   return (float)tmp/p;     } }

use bigdecimal class instead of float.

and use java code this:

bigdecimal bd = new bigdecimal(floatval); bd = bd.setscale(2, bigdecimal.round_half_up); 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -