float per = (num / (float) totbrwdbksint) * 100;
im gets the value per, as they say 29.475342. I want it to be rounded to two decimal places. Like 29.48. How to achieve this?
You must do this as part of the formatting - the floating point number itself does not have the concept of “two decimal places”.
For example, you can use DecimalFormatwith the drawing "# 0.00":
DecimalFormat
import java.text.*; public class Test { public static void main(String[] args) { float y = 12.34567f; NumberFormat formatter = new DecimalFormat("#0.00"); System.out.println(formatter.format(y)); } }
As John implies , the format to display. The most concise way to do this is probably with a class String.
String
float f = 70.9999999f; String toTwoDecPlaces = String.format("%.2f", f);
This will result in a line "71.00"
"71.00"
, , , BigDecimal ist . http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html
, java, , , . , , , :
, /... , ... ... , 29.475342 → 29.47 , ?,.005 /.
, ... , , XX.XXXXXXXXXX(, , 27/28 ), XX.XX.
- ...
, , , .
you can use the formatted printing method System.out.printf to format the print if you need it
Source: https://habr.com/ru/post/1727856/More articles:XML data mapping - xmlAndroid MapView not working - androidModels in View Presenter MVP - design-patternsMake sure that a space exists after any occurrence of a specific char in a string - c #cocoa objective-c для os x: получить точку монтирования тома из пути - objective-cJavaScript getElementsByCustomTag ('value')? - javascriptHow to make maven-pmd-plugin support the latest version of PMD? - maven-2Automatic removal of Delphi IFDEf compiler directives - delphiParsing strings in the .NET Console - command-lineReplace selected text in Firefox - javascriptAll Articles