问题
I have a variable of double type representing a value of about 34.323423423123456. I'd like to format it to have only two decimal places (e.g. 34.32), rounded properly; e.g. 2.229934345 would become 2.23.
I am new to Java and none the tricks I know from C# worked.
回答1:
Related to this Question use DecimalFormat setRoundingMode
回答2:
Here's an example format string, I believe it correctly rounds the resulting value. See Markus' post if you need more control over the rounding or format:
String.format("%.2f", 2.229934345);
来源:https://stackoverflow.com/questions/649447/how-to-format-a-variable-of-double-type