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

Java Tutorial #6 – Jump Statements

Introduction The break statement is used to stop further execution. It can be used either in a loop or within a switch statement. Break Statement The break...

How To Create Singleton Class in Java

Design patterns Overview Design patterns are basically solutions to programming problems that developers normally encounter during software development. Design patterns were first documented by four...

Rounding Decimal Number With Java DecimalFormat

The DecimalFormat class has a method called setRoundingMode() which can be used for setting the rounding mode for DecimalFormat object. The setRoundingMode() accepts RoundingMode...

Â

MORE IN THIS CATEGORY

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...

How To Create Maven Project Using Command Line

Maven project can be easily created using built-in plugins from the popular IDEs such as Eclipse and IntelliJ IDEA. However, in scenarios where you...

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...

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