简体   繁体   English

在Java中从BigDecimal中删除尾随零

[英]Removing trailing zeros from BigDecimal in Java

I need to remove trailing zeros from BigDecimal along with RoundingMode.HALF_UP . 我需要从BigDecimal删除尾随零以及RoundingMode.HALF_UP For instance, 例如,

Value        Output

15.3456  <=> 15.35
15.999   <=> 16            //No trailing zeros.
15.99    <=> 15.99
15.0051  <=> 15.01
15.0001  <=> 15           //No trailing zeros.
15.000000<=> 15           //No trailing zeros.
15.00    <=> 15           //No trailing zeros.

stripTrailingZeros() works but it returns scientific notations in situations like, stripTrailingZeros()有效,但它会在类似的情况下返回科学记号,

new BigDecimal("600.0").setScale(2, RoundingMode.HALF_UP).stripTrailingZeros();

In this case, it returns 6E+2 . 在这种情况下,它返回6E+2 I need this in a custom converter in JSF where it might be ugly for end users. 我需要在JSF中的自定义转换器中使用它,这对最终用户来说可能很难看。 So, what is the proper way of doing this? 那么,这样做的正确方法是什么?

Use toPlainString() 使用toPlainString()

BigDecimal d = new BigDecimal("600.0").setScale(2, RoundingMode.HALF_UP).stripTrailingZeros();
System.out.println(d.toPlainString()); // Printed 600 for me

I'm not into JSF (yet), but converter might look like this: 我还没有进入JSF,但是转换器可能看起来像这样:

@FacesConverter("bigDecimalPlainDisplay")
public class BigDecimalDisplayConverter implements Converter {
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        throw new BigDecimal(value);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        BigDecimal  bd = (BigDecimal)value;
        return bd.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
    }
}

and then in xhtml: 然后在xhtml中:

<h:inputText id="bigDecimalView" value="#{bigDecimalObject}" 
    size="20" required="true" label="Value">
    <f:converter converterId="bigDecimalPlainDisplay" />
</h:inputText>

Note that stripTrailingZeros() doesn't do very well either. 请注意, stripTrailingZeros()也不是很好。

On this: 在这:

val = new BigDecimal("0.0000").stripTrailingZeros();
System.out.println(val + ": plain=" + val.toPlainString());

val = new BigDecimal("40.0000").stripTrailingZeros();
System.out.println(val + ": plain=" + val.toPlainString());

val = new BigDecimal("40.50000").stripTrailingZeros();
System.out.println(val + ": plain=" + val.toPlainString());

Output (Java 7): 输出(Java 7):

0.0000: plain=0.0000
4E+1: plain=40
40.5: plain=40.5

Output (Java 8): 输出(Java 8):

0: plain=0
4E+1: plain=40
40.5: plain=40.5

The 0.0000 issue in Java 7 is fixed in Java 8 by the following java fix . Java 7中的0.0000问题通过以下java修复在Java 8中得到修复

You can also accomplish this with String.format() , like so: 您也可以使用String.format()完成此操作,如下所示:

final BigDecimal b = new BigDecimal("600.0").setScale(2, RoundingMode.HALF_UP);
String f = String.format("%.0f", b);
System.out.println(f); //600

If you want to do this at your BigDecimal object and not convert it into a String with a formatter you can do it on Java 8 with 2 steps: 如果要在BigDecimal对象上执行此操作而不将其转换为带有格式化程序的String,则可以在Java 8上执行以下两个步骤:

  1. stripTrailingZeros() stripTrailingZeros()
  2. if scale < 0 setScale to 0 if don't like esponential/scientific notation 如果不喜欢指数/科学记数法,如果scale <0 setScale为0

You can try this snippet to better understand the behaviour 您可以尝试使用此代码段来更好地了解该行为

BigDecimal bigDecimal = BigDecimal.valueOf(Double.parseDouble("50"));
bigDecimal = bigDecimal.setScale(2);
bigDecimal = bigDecimal.stripTrailingZeros();
if (bigDecimal.scale()<0)
    bigDecimal= bigDecimal.setScale(0);
System.out.println(bigDecimal);//50
bigDecimal = BigDecimal.valueOf(Double.parseDouble("50.20"));
bigDecimal = bigDecimal.setScale(2);
bigDecimal = bigDecimal.stripTrailingZeros();
if (bigDecimal.scale()<0)
    bigDecimal= bigDecimal.setScale(0);
System.out.println(bigDecimal);//50.2
bigDecimal = BigDecimal.valueOf(Double.parseDouble("50"));
bigDecimal = bigDecimal.setScale(2);
bigDecimal = bigDecimal.stripTrailingZeros();
System.out.println(bigDecimal);//5E+1
bigDecimal = BigDecimal.valueOf(Double.parseDouble("50.20"));
bigDecimal = bigDecimal.setScale(2);
bigDecimal = bigDecimal.stripTrailingZeros();
System.out.println(bigDecimal);//50.2

You should make some calculation on the BigDecimal, and then round it half up eg 你应该对BigDecimal进行一些计算,然后将它向上舍入一半

    BigDecimal toPay = new BigDecimal(1453.00005);
    toPay = toPay.multiply(new BigDecimal(1)).setScale(2, RoundingMode.HALF_UP)

It worked for me. 它对我有用。

You can use DecimalFormat . 您可以使用DecimalFormat For example: 例如:

BigDecimal value = new BigDecimal("15.3456").setScale(2, BigDecimal.ROUND_HALF_UP));
String valueString = new DecimalFormat("#.##").format(value);
System.out.println(valueString); //15.35

Please try yourself. 请试一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM