I use this regular expression to remove trailing zeros from numbers with dot(.
) delimiter:
(?:(\.\d*[1-9])|\.)0*$
and I'm replacing with $1
.
example:
it will give me:
But it doesn't seem to work the same way with comma(,
). I tried this but with no luck:
(?:(\,\d*[1-9])|\,)0*$
I'm new to PHP and this is my first question here. Thank you in advance.
If these are separate strings, numbers only, then use: [.,]?0*$
and replace it with an empty string.
Assuming you mean to replace the trailing zeros in decimal numbers with commas like 1,50 and not numbers using commas to group digits into triplets, then search for (\d*[.,](?:\d*[1-9]|))0*$
and replace it with $1
If the regex doesn't match your string, then there is no dot or comma present, which means there's no fractional part of the number, so you don't want to remove the trailing zeros because they are significant.
Remember that with regex, you need to escape the dot because it's a special symbol (except when between brackets), but you shouldn't escape the comma