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,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- Validate email address in Python using regular expression (regex) - Python
- How to the Install Official Android Emulator for Mac? - Android
- List of Programming Languages Supported by Notepad++ - NotepadPlusPlus
- Mac: How to Merge Cells in Excel - MacOS
- Enable Spellcheck in eclipse workspace - Eclipse
- zsh hello world example - Linux
- Keyboard shortcuts to format Source code in Microsoft Visual Studio Code IDE - Microsoft
- How to mute or disable Twitter Fleet in feeds - Twitter