Trim More than Two Trailing Zeros in BigDecimal

What would be a good way to trim more than two trailing zeros for BigDecimal

So 1.2200 will print 1.22 and 1.0000 will print 1.00

Change as well as return 1.222200 as 1.2222 and 1.220000001 as 1.220000001, etc. Therefore, ignoring the first two zeros, I want to trim any incoming 0s and not trim non-zero values

One way might be to multiply and then use the built-in trimmer trailing zeros, and then divide by 100. This may be problematic with angular cases, but the values ​​in my problem are based on currency and will never exceed the boundaries set by Java (or that means my software deals with offers that are in US dollars)

An ugly solution looks like

System.out.println(((new BigDecimal("1.230223000") .multiply(new BigDecimal("100.0")) .stripTrailingZeros()).divide(new BigDecimal("100.0")))); 
+6
source share
6 answers

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)); } } 
+8
source

Update . Having these mixed requirements (i.e. at least 2 digits after the decimal point should be displayed, but as many as you like) is not trivially implemented, but you can get closer:

Combine stripTrailingZeros() with DecimalFormat to get the desired behavior (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 two digits after the decimal point and up to ten digits after the decimal point if this improves accuracy.

BigDecimal values ​​with more than 10 digits after the decimal point is still disabled:

  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 decimal point and know that you will not lose accuracy in this way, 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 raise an ArithmeticException when rounding is required (i.e., anything after the first two digits is non-zero).

If your values ​​may have 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)); 

It will open 1.24 .

If you don't know the exact number of digits, but want as little as possible (i.e. you want the smallest possible scale for your BigDecimal ), calling stripTrailingZeros() will do exactly what you want:

 System.out.println(new BigDecimal("1.230001000").stripTrailingZeros(); 

This will print 1.230001 .

+13
source

This method will give you the result you want (money round): (what is String, because it is better for BigDecimal see the documentation)

 public static float roundUp(String what, int howmuch) throws Exception{ try { return (new BigDecimal(what).setScale(howmuch, BigDecimal.ROUND_UP)).floatValue(); } catch (NumberFormatException nfe) { throw new Exception("BigDecimal cannot parse value : " + what, nfe); } } 
0
source

If for display purposes use:

 BigDecimal d = new BigDecimal("1.2200"); NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US); String s = n.format(d.doubleValue()); 
0
source

To output String use DecimalFormat .

Otherwise use this:

 public static BigDecimal stripToMinimumScale(BigDecimal value, final int minimumScale) { if (value.scale() == minimumScale) // Already correct scale return value; else { value = value.stripTrailingZeros(); return (value.scale() < minimumScale) ? value.setScale(minimumScale) : // Too few decimals, needs zero pad value; // Do not round any significant digits } } 
0
source
 BigDecimal d = new BigDecimal("59.0000"); String d1 = new DecimalFormat().format(d); System.out.println("d1 is " + d1); 
-1
source

Source: https://habr.com/ru/post/889375/


All Articles