Setting Max/Min Digits For Decimal Numbers With Java DecimalFormat

Set minimum and maximum digits for decimal number with java.text.DecimalFormat

The DecimalFormat class offers the following four such methods which can be used to easily set the maximum and/or minimum digits for decimal numbers.

As shown in the below table, two of these methods can set max/min digits for integer part while the other two methods can do the same for the fraction part of the decimal numbers as is evident from the method names.

setMaximumFractionDigits​(int newValue) Sets the maximum number of digits allowed in the fraction portion of a number
setMaximumIntegerDigits​(int newValue) Sets the maximum number of digits allowed in the integer portion of a number
setMinimumFractionDigits​(int newValue) Sets the minimum number of digits allowed in the fraction portion of a number
setMinimumIntegerDigits​(int newValue) Sets the minimum number of digits allowed in the integer portion of a number

The following example demonstrates the usage of above methods:

The above code produces the following output:

Original Num: 170180.24551
JVM Locale Formatted Num: 170,180.246
Pattern Formatted Num: 17,0180.2455
With Min/Max Fraction Digits: 17,0180.2455
With Min/Max Integer & Fraction Digits: 180.2455
Tushar Sharma
Tushar Sharmahttps://www.automationdojos.com
Hi! This is Tushar, the author of 'Automation Dojos'. A passionate IT professional with a big appetite for learning, I enjoy technical content creation and curation. Hope you are having a good time! Don't forget to subscribe and stay in touch. Wishing you happy learning!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

RELATED POSTS

Concatenate Strings Using String.concat()

The String class has a method called concat. It accepts as parameter a String value and concatenates it with the current String. The following...

How To Create Maven Project in Eclipse With Archetype

Maven Basics Maven automates the steps involved in building a software application like adding the JAR files, compiling code, running unit tests, creating the output...

How To Sort List in Java

Introduction A List is an interface in the Java collection framework. It can be used to store objects. Programmers often encounter scenarios where they need...

Â

MORE IN THIS CATEGORY

Common Git Commands Reference

Git is the most popular distributed version control system which is currently an actively maintained open-source project. Git supports many command-line tools and graphical...

Concatenate Strings Using String.join()

Java 8 has added a static join method to the String class. This concatenates Strings using a delimiter. Performance wise, it is similar to...

Concatenate Strings Using String Joiner

Java 8 has added a new class called StringJoiner. This concatenates Strings using a delimiter as well as adds a prefix and suffix to...

RECENT 'HOW-TO'

How To Install Oh-My-Posh On Windows PowerShell

Oh-My-Posh is a powerful custom prompt engine for any shell that has the ability to adjust the prompt string with a function or variable. It does not...

SIMILAR ON CODEX

- Advertisement -spot_img