What would be a good way to trim more than two trailing zeros for a BigDecimal
So 1.2200 would print 1.22 and 1.0000 would print 1.00
Edit As well as to return 1.222200 as 1.2222 and 1.220000001 as 1.220000001 etc. So disregarding first two zeros I want to trim any incoming 0s and not trim non-zero values
One way could be to multiply, then apply the built in trim trailing zeros and then divide by 100. It could be problematic with corner cases but the values in my problem are currency based and would never exceed the bounds set by Java (or else it means my software is dealing with bids which are in gazzilions of dollars)
The ugly solution is as folows
System.out.println(((new BigDecimal("1.230223000")
.multiply(new BigDecimal("100.0"))
.stripTrailingZeros()).divide(new BigDecimal("100.0"))));
stripTrailingZeros() is an inbuilt method in Java that returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation. So basically the function trims off the trailing zero from the BigDecimal value.
format(doubleVal); // This ensures no trailing zeroes and no separator if fraction part is 0 (there's a special method setDecimalSeparatorAlwaysShown(false) for that, but it seems to be already disabled by default).
Step 1: Get the string Step 2: Count number of trailing zeros n Step 3: Remove n characters from the beginning Step 4: return remaining string.
Update: Having those mixed requirements (i.e. at least 2 digits after the decimal point should be displayed, but as many as necessary) is not trivially implemented, but you can come close:
Combine stripTrailingZeros()
with DecimalFormat
to get the desired behaviour (or close to it):
DecimalFormat df = new DecimalFormat("0.00########")
String formatted = df.format(bigDecimal.stripTrailingZeros())
This will format any BigDecimal
value with at least 2 digits after the decimal point and up to 10 digits after the decimal point, if it improves the precision.
BigDecimal
values with more than 10 digits after the decimal point will still be cut off:
input | output -----------------+---------- 1.20000 | 1.20 1.23000 | 1.23 1.2301 | 1.2301 1.230001000 | 1.230001 1.2300000000001 | 1.23
Original answer:
If you always want to have exactly 2 digits after the comma and know that you won't lose precision this way, then you can call setScale(2, RoundingMode.UNNECESSARY)
:
System.out.println(new BigDecimal("1.23000").setScale(2, RoundingMode.UNNECESSARY));
This code will print 1.23
. Note that this will throw an ArithmeticException
when rounding would be necessary (i.e. anything after the first 2 digits is not zero).
If your values can have a higher precision and you want to apply some rounding, simply replace RoundingMode.UNNECESSARY
with the appropriate value:
System.out.println(new BigDecimal("1.2301").setScale(2, RoundingMode.CEILING));
This will print 1.24
.
If you don't know the exact number of digits but want as few as possible (i.e. you want the smallest possible scale
for your BigDecimal
) then calling stripTrailingZeros()
will do exactly what you want:
System.out.println(new BigDecimal("1.230001000").stripTrailingZeros();
This will print 1.230001
.
Check this,
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class DecimalFormatExample
{
public static void main(String args[])
{
double amount = 2192.015;
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println("The Decimal Value is:"+formatter.format(amount));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With