Search code examples
regexactionscript-3

Using RegEx how do I remove the trailing zeros from a decimal number


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.


Solution

  • Regex:

    ^(\d+\.\d*?[1-9])0+$
    

    OR

    (\.\d*?[1-9])0+$
    

    Replacement string:

    $1
    

    DEMO

    Code:

    var result:String = theStringOfTheNumber.replace(/(\.\d*?[1-9])0+$/g, "$1" );