How to round double or float values to 2 decimal points

Sometimes while working with double and floats, we need to round them to specific decimal points for calculation. For example, stores round final price to 2 decimal places with half-up rounding mode.

Example #1

Input : 12

output : 12.00

Example #2

Input: 18.888

output : 18.89

1. This example using console input with double

Outputs

Test #1

Please enter number : 12
Round off number up to 2 decimal place : 12.00

Test #2

Please enter number : 18.888
Round off number up to 2 decimal place : 18.89

2.  Using float variable

A.  Formula of example is to round of up to 2 decimal points

Round off

Formate

B. Formula of example is to round of up to 3 decimal points

Round off

Formate

 

Uses : Sometimes in project we needed show pricing in $189.00 or ₹123.00 format.

It can be also interview question for Java and android developers.


One thought on “How to round double or float values to 2 decimal points

Leave a Reply

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