If you do not want decimals places for numeric values whose precision has been set (to say 2 decimal places) when it has trailing zeros
e.g. 2022 instead of 2022.00
DecimalFormat class is the good way to achieve it.
Sample Program:
import java.math.BigDecimal;
import java.text.DecimalFormat;
public class NoTrailingZerosEg {
public static void main(String[] args) {
BigDecimal number1 = new BigDecimal(2014.100);
BigDecimal number2 = new BigDecimal(2014.06);
BigDecimal number3 = new BigDecimal(2014.00);
DecimalFormat decFormat = new DecimalFormat();
decFormat.setMaximumFractionDigits(2);
decFormat.setMinimumFractionDigits(0);
System.out.println(decFormat.format(number1));
System.out.println(decFormat.format(number2));
System.out.println(decFormat.format(number3));
}
}
Output:
2,014.1
2,014.06
2,014
More Posts related to Java,
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
More Posts:
- How to clear Cache, Cookies and History on iPhone Safari - iOS
- Command to check Last Login or Reboot History of Users and TTYs - Linux
- Detect if Cookies are enabled using JavaScript - JavaScript
- Hyperlink in html (anchor tag) without a underline - Html
- How to Refresh Mac Desktop - MacOS
- DDMS files not found hprof-conv.exe - Android
- Multiple Microsoft 365 Services Down (Outlook, Teams, SharePoint, OneDrive) - 25 January 2023 (Solved) - Microsoft
- How to find the path of formula Installation by Brew - HowTos