I have some float variables in my java program:
float var1=1.23456f;
float var2=2.34567f;
In my calculations, the number of digits after decimal point in my float variable increases and decreases to maintain precision. for e.g. after some calculations System.out.println(var1); may print:
6.35
or
9.4500082E
or
88.25214
I want to round off these values to 3 decimal places and drop the subsequent decimal digits so that float gets rounded off like:
6.350 9.450 88.252
As far as i know, The NumberFormat and String.format() return the output as formatted String. How can i get the output as rounded off float to use it further in my calculations? Can i apply a rule to my float(Float) variables(Instance) to always round-off/drop the trailing digits after 3 decimal places?
float
or even adouble
. What would you have the program do in that case? Find the closest number that can be represented to 3dp? This would cause massive rounding error... – Boris the Spiderfloat
doesn't store a decimal number. – Louis Wasserman