1

I have to Remove Zero at the end of string only if string have , in it. Can i do all below using Regex. So

123,90 -> 123,9
123,00 -> 123,
123,0 -> 123,
123,0090 ->123,009
00,34-> 00,34
ABC -> ABC
1
  • 2
    Perhaps, s.Contains(",") ? s.TrimEnd('0') : s Commented Jul 7, 2016 at 14:33

5 Answers 5

1

Search using this regex:

(,\d*?)0+\b

and replace it by:

$1

IdeOne Code demo

RegExStrorm Demo - Click on "Context" tab to see results

RegEx101 Demo

Sign up to request clarification or add additional context in comments.

8 Comments

Hey anu. Tis works perfect. But how to adjust this expression to remove from start also. 00123,2500 -> 123,25
Hey but it break when 0001869,0258 . Should be 1869,0258 but coming as 0001869,0258
What will happen to 00,34 in your question?
It should come up as ,34. but should not change anything if no comma . This works as of now
|
1

Remember than RegEx is used to test a pattern. It will return true or false rather than perform any changes to the string itself.

If you want to search for characters at the end of a string, you can use the $ sign to build your regex

enter image description here

Use this tool if you want to test this live

So, this RegEx will return true each time you have a 0 at the end, false otherwise

0$

Now, if you need to remove that zero, you need to perform a TRIM

"129,0".TrimEnd('0')

Note the quotes within the TrimEnd function. This is because this method expects a char an not a string.

Regards! Rod

Comments

0
     String line = "120,90";


    String newLine = line.replaceAll("0*$", "");
//120,9

removes any '0' starting from the end of the string expression. Replaces all 0s with empty space from the end of the string up to the first non 0 character in the string.

Comments

0

It seems as if you use , as number-decimal-separator. So you could use double.TryParse and double.ToString to get the desired result:

string str = "123,0090";
double d;
if (double.TryParse(str, out d))
    str = d.ToString();

If you want to ensure that comma is used as decimal separator:

if (double.TryParse(str, NumberStyles.Any, new CultureInfo("de-DE"), out d))
    str = d.ToString();

If you'd use decimal.TryParse instead the number of trailing zeros would be preserved which isn't desired in this case. Msdn: "The scaling factor also preserves any trailing zeros in a Decimal number. ....might be revealed by the ToString method...."

Comments

0

To do it in Javascript you can do as follow :

'0.000015000000'.replaceAll(/0*$/g, "") 

Return "0.000015"

1 Comment

That's not a JS question, it's marked as .net

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.