Reputation: 2034
I'm needing to write some regex that takes a number and removes any trailing zeros after a decimal point. The language is Actionscript 3. So I would like to write:
var result:String = theStringOfTheNumber.replace( [ the regex ], "" );
So for example:
3.04000
would be 3.04
0.456000
would be 0.456
etc
I've spent some time looking at various regex websites and I'm finding this harder to resolve than I initially thought.
Upvotes: 16
Views: 33399
Reputation: 146
None of these worked for my case, so here's another go at this question:
/((?:[^\d.]|^)\d*\.\d*?)(0+)(?=(?:[^\d.]|$))/gm
Replace this with the first capture group $1
to trim trailing zeroes after the decimal for all numbers in the target string.
Here's a breakdown of how it works:
(?:[^\d.]|^)
=> The character right before a number, or lack thereof if it's the beginning of the string.\d*\.
=> Everything up to and including the decimal point.\d*?
=> This needs to be lazy (?
) to let the next pattern consume the trailing zeroes:(0+)
=> Matches the trailing zeroes.(?=(?:[^\d.]|$))
=> The character at the end of a number, or lack thereof if it's the end of the string.Here's a demo on Regex101 ➚
The above regex will not trim the decimal point itself (1.00 => 1.
), so an additional substitution might be necessary depending on your needs:
/(\d)(?:\.)(\D|$)/gm
Replace this with $1$2
to trim the unnecessary decimal points.
Upvotes: 0
Reputation: 9
'1.00020'.replace(/\.0+$|(?<=\.\d*?)0+$/, '')
match 0 0.00 1.00 1.001 1.00100
Upvotes: -1
Reputation: 69
I tested few answers from the top:
^(\d+\.\d*?[1-9])0+$
(\.\d*?[1-9])0+$
(\.\d+?)0+\b
All of them not work for case when there are all zeroes after "." like 45.000
or 450.000
modified version to match that case: (\.\d*?[1-9]|)\.?0+$
also need to replace to '$1' like:
preg_replace('/(\.\d*?[1-9]|)\.?0+$/', '$1', $value);
Upvotes: 1
Reputation: 49
I know I am kind of late but I think this can be solved in a far more simple way. Either I miss something or the other repliers overcomplicate it, but I think there is a far more straightforward yet resilient solution RE:
([0-9]*[.]?([0-9]*[1-9]|[0]?))[0]*
By backreferencing the first group (\1) you can get the number without trailing zeros.
It also works with .XXXXX... and ...XXXXX. type number strings. For example, it will convert .45600 to .456 and 123. to 123. as well.
More importantly, it leaves integer number strings intact (numbers without decimal point). For example, it will convert 12300 to 12300.
Note that if there is a decimal point and there are only zeroes after that it will leave only one trailing zeroes. For example for the 42.0000 you get 42.0.
If you want to eliminate the leading zeroes too then youse this RE (just put a [0]* at the start of the former):
[0]*([0-9]*[.]?([0-9]*[1-9]|[0]?))[0]*
Upvotes: 1
Reputation: 1646
Other answers didn't consider numbers without fraction (like 1.000000
) or used a lookbehind function (sadly, not supported by implementation I'm using). So I modified existing answers.
^-?\d+(\.\d*[1-9])?
- Demo (see matches). This will not work with numbers in text (like sentences).\1
or $1
) using (^-?\d+\.\d*[1-9])(0+$)|(\.0+$)
- Demo (see substitution). This one will work with numbers in text (like sentences) if you remove the ^
and $
.Both demos with examples.
Side note: Replace the \.
with decimal separator you use (,
- no need for slash) if you have to, but I would advise against supporting multiple separator formats within such regex (like (\.|,)
). Internal formats normally use one specific separator like .
in 1.135644131
(no need to check for other potential separators), while external tend to use both (one for decimals and one for thousands, like 1.123,541,921
), which would make your regex unreliable.
Update: I added -?
to both regexes to add support for negative numbers, which is not in demo.
Upvotes: 2
Reputation: 976
I'm a bit late to the party, but here's my solution:
(((?<=(\.|,)\d*?[1-9])0+$)|(\.|,)0+$)
My regular expression will only match the trailing 0s, making it easy to do a .replaceAll(..)
type function.
Breaking it down, part one: ((?<=(\.|,)\d*?[1-9])0+$)
(?<=(\.|,)
: A positive look behind. Decimal must contain a .
or a ,
(commas are used as a decimal point in some countries). But as its a look behind, it is not included in the matched text, but still must be present.\d*?
: Matches any number of digits lazily[1-9]
: Matches a single non-zero character (this will be the last digit before trailing 0s)0+$
: Matches 1 or more 0
s that occur between the last non-zero digit and the line end.This works great for everything except the case where trailing 0s begin immediately, like in 1.0
or 5.000
. The second part fixes this (\.|,)0+$
:
(\.|,)
: Matches a .
or a ,
that will be included in matched text.0+$
matches 1 or more 0
s between the decimal point and the line end.Examples:
1.0
becomes 1
5.0000
becomes 5
5.02394900022000
becomes 5.02394900022
Upvotes: 0
Reputation: 1960
What worked best for me was
^([\d,]+)$|^([\d,]+)\.0*$|^([\d,]+\.[0-9]*?)0*$
For example,
s.replace(/^([\d,]+)$|^([\d,]+)\.0*$|^([\d,]+\.[0-9]*?)0*$/, "$1$2$3");
This changes
1.10000 => 1.1
1.100100 => 1.1001
1.000 => 1
1 >= 1
Upvotes: 10
Reputation: 724
Is it really necessary to use regex? Why not just check the last digits in your numbers? I am not familiar with Actionscript 3, but in python I would do something like this:
decinums = ['1.100', '0.0','1.1','10']
for d in decinums:
if d.find('.'):
while d.endswith('0'):
d = d[:-1]
if d.endswith('.'):
d = d[:-1]
print(d)
The result will be:
1.1
0
1.1
10
Upvotes: -1
Reputation: 91
If your regular expressions engine doesn't support "lookaround" feature then you can use this simple approach:
fn:replace("12300400", "([^0])0*$", "$1")
Result will be: 123004
Upvotes: 1
Reputation: 39
I know it's not what the original question is looking for, but anyone who is looking to format money and would only like to remove two consecutive trailing zeros, like so:
£30.00 => £30
£30.10 => £30.10 (and not £30.1)
30.00€ => 30€
30.10€ => 30.10€
Then you should be able to use the following regular expression which will identify two trailing zeros not followed by any other digit or exist at the end of a string.
([^\d]00)(?=[^\d]|$)
Upvotes: 0
Reputation: 174696
Regex:
^(\d+\.\d*?[1-9])0+$
OR
(\.\d*?[1-9])0+$
Replacement string:
$1
Code:
var result:String = theStringOfTheNumber.replace(/(\.\d*?[1-9])0+$/g, "$1" );
Upvotes: 20
Reputation: 12389
What about stripping the trailing zeros before a \b
boundary if there's at least one digit after the .
(\.\d+?)0+\b
And replace with what was captured in the first capture group.
$1
Upvotes: 6
Reputation: 108
try this
^(?!0*(\.0+)?$)(\d+|\d*\.\d+)$
And read this http://www.regular-expressions.info/numericranges.html it might be helpful.
Upvotes: 0
Reputation: 67968
(?=.*?\.)(.*?[1-9])(?!.*?\.)(?=0*$)|^.*$
Try this.Grab the capture.See demo.
http://regex101.com/r/xE6aD0/11
Upvotes: 4