Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I round 12.443 to 12.40

Tags:

android

I'm making a calculation and I'm getting 12,443. I want to round it to 12,40. This is the way I'm trying to do it but I'm getting 12 instead of 12,40

float result = Math.round( (new Float(result1)*0.3) + (new Float(result2)*0.7) );
vprosvasis.setText( Float.toString(result) );

Examples:

  • if I get 12,70001 I want to round to 12,7
  • if i get 13,4402 to round it to 13,4
  • 11,19 to 11,2

So, the final number will always be in ##.@ format

like image 556
kostas Avatar asked Dec 01 '22 02:12

kostas


1 Answers

(float)Math.round(value * 10) / 10

should give you the result you want.

like image 163
Robert Harvey Avatar answered Dec 05 '22 00:12

Robert Harvey