So, I'd offer that commaify() meets the demands of versatility and performance though certainly not the most compact or elegant.
![regex for number with decimal in as3 regex for number with decimal in as3](https://i.stack.imgur.com/tXACH.png)
Trace( '\rTesting ' + len + ' random values' ) Ĭommas:int = Math.floor( (front.length - n) / 3 ),įront = front.slice( 0, n ) + "," + front.slice( n ) Then I tested each for accuracy and performance: package Return negative + s + "." + results + "0" Return negative + output + "." + results Return negative + output + "." + results + "0" Var len:Number = input.toString().length
![regex for number with decimal in as3 regex for number with decimal in as3](https://imgs.developpaper.com/imgs/2019062609581328.jpg)
Public static function commaCoder( input:Number ):String Var split:Array = input.toString().split( '.' ),īack:String = ( split.length > 1 ) ? "." + split : null, Public static function commaify( input:Number ):String I put together many of the answers here in a testing class (and included a solution from this blog and an answer of my own called commaify) and formatted them in a consistent way for easy comparison: package Though the currently selected answer does not assume a Number I was curious to find a solution that could and was also more performant than RegExp (which AS3 does not do well). A digit in the range 1-9 followed by zero or more other digits then optionally followed by a decimal point followed by at least 1 digit: 1-9d (.d+). To allow numbers with an optional decimal point followed by digits. While many of these answers work fine with positive integers, many of their argument inputs are cast as Numbers, which implies that they can handle negative values or contain decimals, and here all of the solutions fail. A digit in the range 1-9 followed by zero or more other digits: 1-9d. NumString = numString.substr(0, numString.length - 3) 由于AS3不支持前瞻性,这就是我解决它的方法 public static function formatNumber(number:Number):String I'm getting the feeling I would be better off performing this task using a loop.ĭue to AS3 not supporting lookahead this is how I have solved it. 本练习的重点是在数字中插入三重逗号,如下所示: 10000000 => 10,000,000īut this is not correct due to the search not happening from the back and the replace $1 will only work for the first match. The point of this exercise is to insert triplet commas into a number like so: As far as i can see from the AS3 documentation, searching backwards is not possible in the reg ex engine.
![regex for number with decimal in as3 regex for number with decimal in as3](https://i.stack.imgur.com/Cbu8E.jpg)
Hey there, I'm trying to perform a backwards regular expression search on a string to divide it into groups of 3 digits.