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,
- [Fix] java.time.zone.ZoneRulesException: Unknown time-zone ID
- Parse XML file in Java using DOM Parser
- Java equals method - Tutorial
- [Program] How to read three different values using Scanner in Java
- Java: The value of the local variable string is not used
- Display Output in Java Console as a Table
- How to detect Operating System using Java code
- Java 8 Streams map() with examples
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Add newline character Java code example (\r \n \r\n)
- List of Java Major Minor Version Numbers
- IntelliJ Keyboard Shortcut to remove unused imports [Java]
- Java - Check if array contains the value
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- XmlRpcException ConnectException connection refused error
- Create a Zip file using Java Code programmatically
- List of jar files for Jax-ws (SOAP) based Java Web Services
- How to fix Java HTTP java.net.UnknownHostException
- List of jars required for Struts2 project
- [fix] java: incompatible types: double cannot be converted to java.lang.Integer Generics
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- Get the current timestamp in Java
- java: unclosed string literal [Error]
More Posts:
- How to disable Microsoft teams from startup Windows 10 - Teams
- [Eclipse] Syntax error, annotations are only available if source level is 1.5 or greater - Eclipse
- Steps to Integrate Latest Facebook SDK with your Android Application - Facebook
- SharePoint Server 2016 setup error - A system restart from a previous installation or update is pending. Restart your computer and run setup to continue. - SharePoint
- How do I run HTML file in Notepad? (Windows) - NotepadPlusPlus
- How to Install Git on Windows - Git
- Zsh Shell: Custom alias that you may find useful - MacOS
- Convert byte array to String in Java - Java