Can I do it with System.out.print
?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Look at DecimalFormat
Here is an example from the tutorial:
If you choose a pattern like "###.##", you will get two decimal places, and I think that the values are rounded up. You will want to look at the link to get the exact format you want (e.g., whether you want trailing zeros)
small simple program for demonstration:
You can use
DecimalFormat
. One way to use it:Another one is to construct it using the
#.##
format.I find all formatting options less readable than calling the formatting methods, but that's a matter of preference.
Many people have mentioned
DecimalFormat
. But you can also useprintf
if you have a recent version of Java:See the docs on the Formatter for more information about the printf format string.
OK - str to float.
Output:
Below is code how you can display an output of float data with 2 decimal places in Java: