Remove trailing zero in Java

I have strings (from a DB) that can contain numeric values. If it contains numeric values, I would like to remove trailing zeros, for example:

  • 10.0000
  • 10.234000

str.replaceAll("\\.0*$", "") works on the first, but not on the second.

Many answers point to the use of BigDecimal , but the String I get cannot be numeric. Therefore, I think the best solution probably comes through Regex.

+35
java string regex
source share
11 answers

There are possibilities:

 1000 -> 1000 10.000 -> 10 (without point in result) 10.0100 -> 10.01 10.1234 -> 10.1234 

I'm lazy and stupid, just

 s = s.indexOf(".") < 0 ? s : s.replaceAll("0*$", "").replaceAll("\\.$", ""); 

Same solution using contains instead of indexOf as mentioned in some comments for ease of understanding

  s = s.contains(".") ? s.replaceAll("0*$","").replaceAll("\\.$","") : s 
+72
source share

Use DecimalFormat , its the cleanest way

 String s = "10.1200"; DecimalFormat decimalFormat = new DecimalFormat("0.#####"); String result = decimalFormat.format(Double.valueOf(s)); System.out.println(result); 
+18
source share

I find all the other solution too complicated. Just

 s.replaceFirst("\\.0*$|(\\.\\d*?)0+$", "$1"); 

performs this work. First he tries the first option, so the point followed by all zeros is replaced with nothing (since the group does not receive the set). Otherwise, if he finds a point followed by some digits (as small as possible due to the lazy quantifier *? ), Followed by some zeros, the zeros will be discarded because they are not included in the group. He is working .

A warning

My code relies on my assumption that adding an unrivaled group does nothing . This is true for an Oracle implementation, but not for others, including Android , that seem to add the string "null". I would call such implementations broken, as that just doesn't make sense, but they are correct according to Javadoc .

+6
source share
 String value = "10.010" String s = new DecimalFormat("0.####").format(Double.parseDouble(value)); System.out.println(s); Output: 10.01 
+6
source share

Kent-style manipulator response magically works as well as satisfying loss accuracy, but here's a cleaner solution using BigDecimal

 String value = "10.234000"; BigDecimal stripedVal = new BigDecimal(value).stripTrailingZeros(); 

Then you can convert to other types

 String stringValue = stripedVal.toPlainString(); double doubleValue = stripedVal.doubleValue(); long longValue = stripedVal.longValue(); 

If loss of accuracy is your ultimate concern, then get the exact primitive value. This will throw an ArithmeticException if there is some loss of precision for the primitive. See below

 int intValue = stripedVal.intValueExact(); 
+4
source share

The following works for all of the following examples:

 "1" -> "1" "1.0" -> "1" "1.01500" -> "1.015" "1.103" -> "1.103" s = s.replaceAll("()\\.0+$|(\\..+?)0+$", "$2"); 
+2
source share

How about a replacement

 (\d*\.\d*)0*$ 

by

 \1 

?

0
source share

You can replace with:

 String result = (str.indexOf(".")>=0?str.replaceAll("\\.?0+$",""):str); 

To maintain regular expression as simple as possible. (And credentials like 1000 , as stated in the comments)

0
source share

My implementation with the ability to select numbers after the divisor:

 public static String removeTrailingZero(String number, int minPrecise, char divider) { int dividerIndex = number.indexOf(divider); if (dividerIndex == -1) { return number; } int removeCount = 0; for (int i = dividerIndex + 1; i < number.length(); i++) { if (number.charAt(i) == '0') { removeCount++; } else { removeCount = 0; } } int fracLen = number.length() - dividerIndex - 1; if (fracLen - removeCount < minPrecise) { removeCount = fracLen - minPrecise; } if (removeCount < 0) { return number; } String result = number.substring(0, number.length() - removeCount); if (result.endsWith(String.valueOf(divider))) { return result.substring(0, result.length() - 1); } return result; } 
0
source share
 .replaceFirst("\\.0*$|(\\.\\d*[1-9])0+$", "$1") 
0
source share

First separate the fraction. Then you can use the following logic.

 BigDecimal value = BigDecimal.valueOf(345000); BigDecimal div = new BigDecimal(10).pow(Integer.numberOfTrailingZeros(value.intValue())); System.out.println(value.divide(div).intValue()); 
-one
source share

All Articles