Lazy Number manipulation (such as decimal places or precision)

If you want to manipulate a Number Object (variable) to a specific precision or to fixed decimal places, instead of manipulating the Number itself you can use the Number Class conversion Methods such as toFixed and toPrecision and cast the String Objects they return back to Number Objects:

pi = Number(Math.PI.toFixed(2));
trace(pi); // 3.14

Not a replacement for a dedicated rounding function for mathematical formulae, but a handy shortcut to output rounded Numbers in a DataProvider for example.

p.s. Math.round rounds to Integer Objects, as do Math.floor and Math.ceil which is why this can be useful

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <h3> <h4>
  • Lines and paragraphs break automatically.
  • HTML tags will be transformed to conform to HTML standards.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.