java 如何从字符串中删除尾随零?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16984462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:37:33  来源:igfitidea点击:

How to remove trailing zeros from a string?

javastring

提问by ACP

I have a string salePricewhich can have values like 29.90000,91.01000and i want to get the output like 29.90,91.01as in with the two digits after decimal points. i am using a string.

我有一个字符串salePrice,它可以有像这样的值29.90000,91.01000,我想得到像29.90,91.01小数点后两位数字那样的输出。我正在使用字符串。

回答by Grzegorz ?ur

One of possible solutions

可能的解决方案之一

new BigDecimal("29.90000").setScale(2).toString()

Or if you need to round

或者如果你需要舍入

new BigDecimal("29.90100").setScale(2, RoundingMode.HALF_UP).toString()

Using BigDecimal because converting from String to double can lose precision!

使用 BigDecimal 是因为从 String 转换为 double 会失去精度!

Choose rounding mode that fits your case.

选择适合您情况的舍入模式。

回答by MG_Bautista

Try this...

试试这个...

DecimalFormat df2 = new DecimalFormat( "#,###,###,###.##" );
double dd = 100.2397;
double dd2dec = new Double(df2.format(dd)).doubleValue();

回答by Marvin Emil Brach

int lastIndex = salePrice.indexOf(".") + 2
salePrice = salePrice.substring(0, lastIndex);

回答by Suresh Atta

you can use

您可以使用

String.format("%.2f", value);

回答by giannis christofakis

You can use Apache Commons Mathematics Library

您可以使用Apache Commons Mathematics Library

    NumberFormat nf = NumberFormat.getInstance();
    nf.setMinimumFractionDigits(2);
    nf.setMaximumFractionDigits(2);
    ComplexFormat cf = new ComplexFormat(nf);
    Complex complex = cf.parse("29.90000");

回答by DwB

Here is an old-school way that matches your question (you always want 2 decimal places)

这是一种与您的问题相匹配的老式方法(您总是需要小数点后两位)

public class LearnTrim
{
    public static void main(final String[] arguments)
    {
        String value1 = "908.0100";
        String value2 = "876.1";
        String value3 = "101";
        String value4 = "75.75";
        String value5 = "31.";

        System.out.println(value1 + " => " + trimmy(value1));
        System.out.println(value2 + " => " + trimmy(value2));
        System.out.println(value3 + " => " + trimmy(value3));
        System.out.println(value4 + " => " + trimmy(value4));
        System.out.println(value5 + " => " + trimmy(value5));
    }

    private static String trimmy(final String value)
    {
        int decimalIndex;
        String returnValue;
        int valueLength = value.length(); // use StringUtils.length() for null safety.

        decimalIndex = value.indexOf('.');
        if (decimalIndex != -1)
        {
            if (decimalIndex < (valueLength - 3))
            {
                returnValue = value.substring(0, valueLength - 2);
            }
            else if (decimalIndex == (valueLength - 3))
            {
                returnValue = value;
            }
            else if (decimalIndex == (valueLength - 2))
            {
                returnValue = value + "0";
            }
            else // decimalIndex == valueLength - 1
            {
                returnValue = value + "00";
            }
        }
        else
        {
            returnValue = value + ".00";
        }

        return returnValue;
    }
}