04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Decimal Conversions<br />

Chapter 22: The Evolution of JavaScript<br />

Decimal values can be converted into primitive values using one of the following methods:<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

doubleValue() — Returns the decimal as a floating - point value .<br />

intValue() — Returns the decimal as an integer .<br />

intValueExtract() — Returns the decimal as an integer. If the result isn ’ t a whole number,<br />

then this method throws a RangeError .<br />

toEngineeringString() — Returns the decimal as string, using exponential notation when<br />

necessary .<br />

toPlainString() — Returns the decimal as a string without using exponential notation .<br />

toString() — Returns the decimal as a string .<br />

At least one of these methods must be used to convert the decimal value to a primitive, because the<br />

regular conversion methods such as parseInt() and parseFloat() will not work with decimal values.<br />

Decimal Math<br />

None of the standard methods on the Math object can be used with decimals, so each Decimal value has<br />

the following methods that perform several of these operations:<br />

❑<br />

❑<br />

❑<br />

❑<br />

abs() — Converts the decimal value into its absolute value and returns the result as a new<br />

decimal value. This is used instead of Math.abs() .<br />

min(value) — Returns the lesser of the decimal value and value . This is used instead of<br />

Math.min() .<br />

max(value) — Returns the greater of the decimal value and value . This is used instead of<br />

Math.max() .<br />

pow(n) — Raises the current decimal value to the power specified and returns the result as a<br />

new decimal value. This is used instead of Math.pow() .<br />

There aren ’ t as many options for complex mathematical operations involving decimal values, so they<br />

may be of limited usefulness when working with sines, cosines, and other complex computations.<br />

Other Decimal Methods<br />

There are several other methods available on each instance of Decimal that are unique to decimal<br />

values. They are as follows:<br />

❑<br />

❑<br />

❑<br />

❑<br />

movePointLeft(n) — Returns a new decimal value whose decimal point has been moved<br />

n places to the left.<br />

movePointRight(n) — Returns a new decimal value whose decimal point has been moved<br />

n places to the right.<br />

precision() — Returns the precision of the decimal value as an integer.<br />

scale() — Returns the scale of the decimal value as a new decimal value. The scale is the<br />

number of digits to the right of the decimal point.<br />

755

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!