Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

get 2 digit places

 
Sophia Choi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can I can digit places ? e.g. 12.34534 -> 12.35 or 6.3333-> 6.33
now, I am using the formula as following,
result_arr[i] = (float) Math.floor( (float) tot_weight / respondents * 10 + 0.5) /10 ;
Any other suggestions?
Thanks
 
Barry Burd
Author
Ranch Hand
Posts: 118
5
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
import java.text.NumberFormat;
...
NumberFormat numFormat =
NumberFormat.getNumberInstance();
numFormat.setMinimumFractionDigits(2);
numFormat.setMaximumFractionDigits(2);

System.out.print(numFormat.format(12.34567);
 
The knights of nee want a shrubbery. And a tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!