在Java中从BigDecimal移除尾随零

在Java中从BigDecimal移除尾随零,第1张

在Java中从BigDecimal移除尾随


toPlainString()

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

我还没有进入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();    }}

然后在xhtml中:

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


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5615496.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存