Rounding Decimal Number With Java DecimalFormat

Rounding decimal number with java.text.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 class object as the parameter.

You can pass any of the Enum constants available in the RoundingMode class as parameter to the setRoundingMode() method to achieve the desired rounding of decimal number.

The following example demonstrates rounding of the decimal number:

The above code produces the following output:

170180.24551 Rounded-UP 170,180.246
170180.24551 Rounded-DOWN 170,180.245 
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

Format Decimal Numbers using Strings Within Pattern

As mentioned in earlier posts, the java.text.DecimalFormat class is used to format decimal numbers via predefined patterns specified as String. Apart from the decimal separator,...

Java DecimalFormat Class

When you need to format decimal numbers, such as taking three or two decimal places for a number, showing only the integer part of...

Java Tutorial #4 – Control Statements

Introduction Control statements are used to change the flow of execution based on changes to certain variables in the code. One of the types of...

Â

MORE IN THIS CATEGORY

CronTab Basic Commands With Examples

Cron is a time-based job scheduler in Unix-like operating systems, which is used to automate repetitive tasks. It is a daemon (a background process)...

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 Load XML File into DOM Using MSXML

Microsoft XML Core Services (MSXML) provides two methods for loading XML documents into the Document Object Model (DOM) and one method and one property...

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