## Rounding down to its nearest decimal? BigDecimal vs Alternate Form

**This question already has answers here**:

As stated by commenters: **For financial purposes, use BigDecimal.**

Aside from that:

double netPayCents = Math.floor(netPay * 100); netPay = netPayCents / 100d;

**Why use BigDecimal?**

Floating point values(including double values) can be very precise, but they have issues when trying to be absolutely precise. They are always subject to rounding, after most mathematical operations.

See this thread for more detail: Double vs. BigDecimal?

**Rounding,** All others are rounded to the closest integer. Whenever the fractional part is 0.5, alternate rounding up or down: for the first occurrence of a 0.5 fractional part,� Scaling/rounding operations (setScale and round) return a BigDecimal whose value is approximately (or exactly) equal to that of the operand, but whose scale or precision is the specified value; that is, they increase or decrease the precision of the stored number with minimal effect on its value. Decimal point motion operations (movePointLeft

You can also use the below code:

import java.text.DecimalFormat; import java.math.RoundingMode; public class Main { public static void main(String[] args) { double netPay = 46.225; DecimalFormat f = new DecimalFormat("##.00"); f.setRoundingMode(RoundingMode.DOWN); System.out.println(f.format(netPay)); } }

**Output:**

46.22

**RoundingMode (Java Platform SE 7 ),** Specifies a rounding behavior for numerical operations capable of discarding precision decimal values would round to a one digit decimal value under the rounding a BigDecimal number with the specified value, forming a MathContext object DOWN. public static final RoundingMode DOWN. Rounding mode to round� The java.math.BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The toString() method provides a canonical representation of a BigDecimal. It gives the user complete control over rounding behavior. Two types of operations

Here is the method I use:

For Example netPay value is **46.225**

By using Below code you will get the output like **46.22**

double netPay =46.225; // just assigning your decimal to a variable netPay =netPay *100; // this sets netPay to 4622.5 netPay =Math.floor(netPay); // this sets netPay to 4622.0 netPay =netPay/100; // this sets netPay to 46.22

Vnstead of above code you can just use below single line it will also work the same

netPay =Math.floor(netPay*100) / 100;

Happy Coding !!!!!!!!!!!!!!

**9.4. decimal — Decimal fixed point and floating point arithmetic ,** Unlike hardware based binary floating point, the decimal module has a user alterable Construction from an integer or a float performs an exact conversion of the Context precision and rounding only come into play during arithmetic operations. To make an alternate active, use the setcontext() function. /** * Given an input which has three decimal places, * round it to two decimal places using HALF_EVEN. */ BigDecimal roundToTwoPlaces(BigDecimal n) { // To make sure, that the input has three decimal places. checkArgument(n.scale() == 3); return n.round(new MathContext(2, RoundingMode.HALF_EVEN)); } together with a test like

**decimal — Decimal fixed point and floating point arithmetic ,** The decimal module provides support for fast correctly-rounded decimal floating Construction from an integer or a float performs an exact conversion of the value it may be useful to create alternate contexts using the Context() constructor. Return a copy of the first operand with the sign set to be the same as the sign of� Banker’s rounding — in its most common form — takes the .5 to be rounded and rounds it either up or down so that the result of the rounding is always an even number. Thus 2.5 rounds to 2.0, 3.5 to 4.0, 4.5 to 4.0, 5.5 to 6.0, and so on. Alternate rounding alternates the process for any .5 between rounding down and rounding up.

**Fixed Point Decimal (BigDecimal) Methods,** In addition, when using the BigDecimal type, the rounding rules can be specified a MathContext, or passed directly to the relevant methods of BigDecimal. whose value is the integer part of the quotient (this / divisor) rounded down. Selecting an Alternative Web Service Operation to Expose Forms User's Guide. Yes, scales, rounding and all that might not be able to be set if these operands are used, however a default behavior would be nice (like no rounding, etc). For example: BigDecimal amount = new BigDecimal ("100.05"); BigDecimal discount = amount \* new BigDecimal("0.10"); BigDecimal total = amount - discount; BigDecimal tax = total \* new

-7.6 rounds down to -8; Half Round Down (including negative numbers) When we round 0.5 down we get this: 7.6 rounds up to 8; 7.5 rounds down to 7; 7.4 rounds down to 7-7.4 rounds up to -7-7.5 rounds down to -8-7.6 rounds down to -8 "Symmetric" Rounding. But maybe you think "7.5 rounds up to 8, so -7.5 should go to -8", which is nice and

##### Comments

- Note that really, you should be using BigDecimal, not double.
- @LouisWasserman: The given code does not perform any calculation which cannot be accomplished sufficiently accurate with
`double`

. - @goodvibration
`float`

and`double`

are approximate data types. When dealing with money, approximate is not good enough. - How could I implement BigDecimal, and rounding that as I expected? @LouisWasserman
- The
`100d`

in the denominator should be optional, i.e. just`/ 100`

should work. But other than that, +1 to what I was about to write. - the 'd' is indeed optional, but I prefer to avoid implicit convertions, because then I am more sure that a calculation is not accidentally done in integer or long. It's just personal preference. :)