14

How do I remove trailing zeros in a String value and remove decimal point if the string contains only zeros after the decimal point? I'm using the below code:

String string1 = Double.valueOf(a).toString()

This removes trailing zeros in (10.10 and 10.2270), but I do not get my expected result for 1st and 2nd inputs.

Input

10.0
10.00
10.10
10.2270

Expected output

10
10
10.1
10.227
6
  • try using BigDecimal Commented Aug 28, 2014 at 6:00
  • best bet is regex for such requirement Commented Aug 28, 2014 at 6:04
  • @ScaryWombat The proposed duplicate has a double as the input. This question has a String. Commented Aug 28, 2014 at 7:14
  • 1
    @jdphenix so why does OP do String string1 = Double.valueOf(a).toString() Commented Aug 28, 2014 at 7:22
  • 1
    @ScaryWombat Because I didn't read the question closely enough. Oops. Commented Aug 28, 2014 at 7:23

3 Answers 3

17

The Java library has a built-in class that can do this for it. It's BigDecimal.

Here is an example usage:

BigDecimal number = new BigDecimal("10.2270");  
System.out.println(number.stripTrailingZeros().toPlainString());

Output:

10.227

Note: It is important to use the BigDecimal constructor that takes a String. You probably don't want the one that takes a double.


Here's a method that will take a Collection<String> and return another Collection<String> of numbers with trailing zeros removed, gift wrapped.

public static Collection<String> stripZeros(Collection<String> numbers) {
    if (numbers == null) { 
        throw new NullPointerException("numbers is null");
    }

    ArrayList<String> value = new ArrayList<>(); 

    for (String number : numbers) { 
        value.add(new BigDecimal(number).stripTrailingZeros().toPlainString());
    }

    return Collections.unmodifiableList(value);
}

Example usage:

ArrayList<String> input = new ArrayList<String>() {{ 
    add("10.0"); add("10.00"); add("10.10"); add("10.2270"); 
}};

Collection<String> output = stripZeros(input);
System.out.println(output);

Outputs:

[10, 10, 10.1, 10.227]
Sign up to request clarification or add additional context in comments.

3 Comments

I discovered that stripTrailingZeros() with also a call to toPlainString() results in output that is outside of your spec. I have corrected my answer.
I did not ask the question lol
while technically correct it is not useful for me, case in point: 1.23 becomes 1.229999999999999982236431605997495353221893310546875. So yes, no trailing zeros for sure –
-1

Try

DecimalFormat decimalFormat = new DecimalFormat("#.##");
String string1 = decimalFormat.format(10.000);

1 Comment

This would remove anything beyond the 2nd mantissa digit.
-4

Try regex like this :

public static void main(String[] args) {
    String s = "10.0";
    System.out.println(s.replaceAll("[0]+$", "").replaceAll("(\\.)(?!.*?[1-9]+)", ""));

}

O/P:
10

input :String s = "10.0750";
O/P : 10.075

input : String s = "10.2750";
O/P : 10.275

2 Comments

I don't mind getting downvoted. But I would like to know why?
not the DV, but as someone else said "May not work in every locale. Some use the decimal point as group separators"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.