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
Search using this regex:
(,\d*?)0+\b
and replace it by:
$1
RegExStrorm Demo - Click on "Context" tab to see results
00,34 in your question?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
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
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...."
To do it in Javascript you can do as follow :
'0.000015000000'.replaceAll(/0*$/g, "")
Return "0.000015"
s.Contains(",") ? s.TrimEnd('0') : s