-3

Is this the proper REGEX to remove trailing decimal and zeroes from a string? I can't get it to work. What am I missing?

78.80 -> 78.8

str.replaceAll("^.0*$", "");

i need only 2 decimal points as well like 78.008 should be 78.01

and if it is 78.10 the 78.1 only.

Sam
  • 1
  • 1
  • 1
  • 4
  • @tbsalling even with the same number ;) – Oscar Mederos Feb 22 '13 at 07:39
  • no I have some addition in that... – Sam Feb 22 '13 at 07:40
  • 3
    Remember to show *a representative sample of input*. Also, your "new addition" is nothing like the original. **Changing "78.008" to "78.01" not the same a "remove .. trailing zeros"**. Let's please close this question. Open up another question that asks the "right thing", which might be: "How can I print a float with at most 2 decimals of precision?" (but *make sure to search first*). –  Feb 22 '13 at 07:41
  • Is there any reason why you're not treating it as a float or double? Are you using regex for converting it from `78.008` to `78.01`? hmm.. – Oscar Mederos Feb 22 '13 at 07:45
  • ok but the this is editable ... ok thanks – Sam Feb 22 '13 at 07:46
  • 1
    Don't forget about http://programmers.stackexchange.com/questions/10998/what-does-the-jamie-zawinskis-quotation-about-regular-expressions-mean. Are you trying to print a number with specific precision and just happen to choose RE for that? – denis.solonenko Feb 22 '13 at 07:52

3 Answers3

3

No, the correct regular expression is something more complex, and it needs positive look-behind.

str = str.replaceAll("\\.0*$|(?<=\\.[0-9]{0,2147483646})0*$", "");

you have to escape the ., because otherwise it means "any character", you have to anchor the regex only to the end of the string, and you have to say that the 0 digits must be after a . plus some optional non-zero digits.

Addendum: there is a "special case" that should be handled: 1.00. We handle it by using the |. The first sub-expression means "a dot plus only zeroes" and matches even the dot (that in this way is deleted)

And remember that in Java strings are immutable, so replaceAll will create a new string.

Note the use of {0,2147483646}: if you used a * you would get a Look-behind group does not have an obvious maximum length, because the * would be converted to {0,2147483647} and considering the "+1" length of the \\. it would overflow, so we put the maximum number of digits possible (2147483647, maximum value of a signed int) minus 1 for the dot.

Test example: http://ideone.com/0NDTSq

xanatos
  • 109,618
  • 12
  • 197
  • 280
2

Since you want rounding of the numbers, you should not use regular expressions. You should use DecimalFormat:

DecimalFormat twoDForm = new DecimalFormat("#.#");
Double d = Double.parseDouble("123.078");
String s = twoDForm.format(d);
System.out.println(s);

will output:

123.08
yiannis
  • 1,421
  • 12
  • 21
1

With the anchors ^ and $ you are trying to match the whole string and replace it. And this will not match since there is more in the string.

Try this

 str.replace("0*$", "");

With 0*$ you will match only 0 or more "0" at the end of the string.

Note: This will also change "1000" to "1"!

stema
  • 90,351
  • 20
  • 107
  • 135