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

How To Convert List To Array in Java

A common scenario faced by programmers is to convert a Java List to an Array. A 'List' is part of the Collection Framework and...

How To Remove Duplicates From List in Java

Introduction A List is an interface in the Java collection framework which can be used to store a group of objects. A List is ordered...

Getting Started With Hibernate

Introduction Hibernate is a framework which allows you to access database tables from Java code. Before Hibernate came into existence, JDBC was widely used. JDBC...

Â

MORE IN THIS CATEGORY

Convert String to Date Using SimpleDateFormat Class

Sometimes, you may need to convert a String to a java.util.Date object. For this, you need to use the SimpleDateFormat class. You need to...

How To Remove Duplicates From List in Java

Introduction A List is an interface in the Java collection framework which can be used to store a group of objects. A List is ordered...

Concatenate Strings Using StringBuilder.append()

In addition to String, Java has the java.lang.StringBuffer and java.lang.StringBuilder classes. These can also be used to concatenate Strings. The advantage of these...

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