In AS3, from a division I get a number like this one: 0.9130406010219044. Is there any way to reduce the number of decimals (aside from multiplying that number for one million)? Is there a way to reduce the numbers BEFORE the division is performed?
6 Answers
Got the following function from this link, which rounds to an arbitrary number of decimals:
public function trim(theNumber:Number, decPlaces:Number) : Number {
if (decPlaces >= 0) {
var temp:Number = Math.pow(10, decPlaces);
return Math.round(theNumber * temp) / temp;
}
return theNumber;
}
// Round a number to two decimal places trace(trim(1.12645, 2));
// Displays: 1.13
Note: I slightly changed the function definition by adding types. See the link for explanation and original source code. Also made it return theNumber
if decPlaces
is less than or equal to zero.

- 1,014
- 1
- 7
- 12
-
I made myself something very similar to this one: I was looking for a built-in solution, but it seems only the latest versions of the Player support it. – Aug 03 '11 at 16:05
-
Well, not if you worry about performance. The trim method is just over three times faster than toFixed(tested by running the methods a million times with the same value and 2 decimals - same result if I run it 10000 times or ten million times too), not including the toFixed conversion back to Number again. – Bakapii Aug 03 '11 at 20:02
-
Bakapi, this one could be even faster: return Number(String(numberToTrim).substring(0,totalChars))... – Aug 04 '11 at 07:36
-
2Actually doing this `Math.round(EVAL_NUMBER * 100) / 100;` is by far the fastest method, second is the `trim` method above, followed by `toFixed` and by far the slowest is the method involving `substr` (which also doesn't round and doesn't work if you have more than one digit before the decimal point). All methods tested by running the same expression one million times. – Bakapii Aug 04 '11 at 08:40
-
For comparison, using the `Math.round(..)` method is about 23 times faster than the `substr` method and about ten times faster than using `toFixed`. – Bakapii Aug 04 '11 at 08:47
-
According to you, is it possible to reduce the number of decimals BEFORE the division is performed? – Aug 04 '11 at 10:29
-
Not if you're working with irrational numbers. Besides it's probably easier and faster to just round the result, of couse very much depending on what you intend to do with it. – Bakapii Aug 04 '11 at 11:57
var myNumber:Number = 74.559832;
trace(myNumber.toFixed(4)); //74.5598
trace(myNumber.toFixed(2)); //74.56
AS3 Documentation: Number class

- 16,553
- 15
- 84
- 162
-
-
-
TheDarkin, I know you can do this, but it is incredible a Number method returns a string: in practice, if I convert a large number to a String and I extract a substring I get the same as toFixed... – Aug 04 '11 at 07:29
-
i'm assuming there is good reason why it's converted to a string, but maybe not. it would be a good question on SO if you feel like posting it. – Chunky Chunk Aug 04 '11 at 12:06
If you just want to display the result (you didn't specify) then a simple bit of String manipulation will yield the fastest result:
0.9130406010219044.toString().substr(0, 4); // 0.91

- 6,119
- 2
- 26
- 28
Take a look at NumberFormatter.fractionalDigits
Or, if you're working in Flex: mx:NumberFormatter.precision / s:NumberFormatter.fractionalDigits

- 10,246
- 2
- 40
- 48
Try some of the answers here on for size:
How to deal with Number precision in Actionscript?
If you use a NumberFormatter, make sure to specify rounding (it's most likely you'll want nearest).
-
1NumberFormatter is possible only if you target the Flash Player 10.1 and above. – Aug 03 '11 at 16:04
If you need Number as result and performance, I would say this solution is more efficient than the Math.pow() If you need 3 decimals just change 100 by 1000.
var myNumber:Number = 3.553366582;
myNumber = (( myNumber * 100 + 0.5) >> 0) / 100;
//trace = 3.55
demonstrating the rounding :
var myNumber:Number = 3.557366582;
myNumber = (( myNumber * 100 + 0.5) >> 0) / 100;
//trace = 3.56
Regarding the Number.toFixed() returning a String I guess it's because it returns 2 decimals in any case: For instance :
Number(3).toFixed(2); // trace 3.00 so it has to be a String.

- 476
- 1
- 4
- 10