Number Functions and Properties

Overview

Number methods provide ways to format and represent numeric values as strings. These methods are called on Number objects and allow you to control the precision, notation, and format of numeric output. All methods work similarly to their JavaScript Number prototype equivalents.

toExponential()

Description:

Returns a string representing the Number object in exponential (scientific) notation.

This is similar to the JavaScript Number.prototype.toExponential() method.

Syntax:

number.toExponential()
number.toExponential(fractionDigits)

Parameters:

  • fractionDigits (optional) - An integer specifying the number of digits to appear after the decimal point. If omitted, as many digits as necessary are used.

Returns: A string representing the number in exponential notation

Examples:

// Without specifying digits
Math.random().toExponential()       // Returns: "7.650264534953171e-1"

// With 2 decimal places
Math.random().toExponential(2)      // Returns: "7.65e-1"

// Large number
(12345.6789).toExponential()        // Returns: "1.23456789e+4"

// Small number
(0.000123).toExponential(2)         // Returns: "1.23e-4"

// Field value
$amount.toExponential(3)            // Format field value with 3 decimal places

Use Cases:

  • Scientific calculations requiring exponential notation
  • Representing very large or very small numbers compactly
  • Data analysis and statistical reporting
  • Engineering and scientific data formatting

toFixed()

Description:

Returns a string representing the Number object using fixed-point notation with a specified number of decimal places.

This is similar to the JavaScript Number.prototype.toFixed() method.

Syntax:

number.toFixed()
number.toFixed(digits)

Parameters:

  • digits (optional) - An integer specifying the number of digits to appear after the decimal point. Range: 0 to 20. Default is 0.

Returns: A string representing the number in fixed-point notation

Examples:

// Round to nearest integer (default)
(0.89473).toFixed()                 // Returns: "1"

// Two decimal places (currency)
(45.6789).toFixed(2)                // Returns: "45.68"

// No decimal places
(123.456).toFixed(0)                // Returns: "123"

// Padding with zeros
(5).toFixed(2)                      // Returns: "5.00"

// Field value with 2 decimals
$price.toFixed(2)                   // Format price with 2 decimal places

Use Cases:

  • Currency formatting (e.g., $45.67)
  • Displaying percentages with fixed precision
  • Rounding numbers for display purposes
  • Ensuring consistent decimal places in reports
Important: The toFixed() method performs rounding. For truncation without rounding, use Math.trunc() first.

toPrecision()

Description:

Returns a string representing the Number object to the specified precision (total number of significant digits).

This is similar to the JavaScript Number.prototype.toPrecision() method.

Syntax:

number.toPrecision()
number.toPrecision(precision)

Parameters:

  • precision (optional) - An integer specifying the number of significant digits. Range: 1 to 21. If omitted, the number is converted to a string without formatting.

Returns: A string representing the number with the specified precision

Examples:

// 2 significant digits
(4.2103).toPrecision(2)             // Returns: "4.2"

// 4 significant digits
(4.2103).toPrecision(4)             // Returns: "4.210"

// Large number with 3 significant digits
(12345.6789).toPrecision(3)         // Returns: "1.23e+4"

// Small number with 5 significant digits
(0.000123456).toPrecision(5)        // Returns: "0.00012346"

// Without precision (no formatting)
(123.456).toPrecision()             // Returns: "123.456"

// Field value with precision
$measurement.toPrecision(3)         // Format with 3 significant digits

Use Cases:

  • Scientific measurements requiring specific significant figures
  • Quality control and manufacturing tolerances
  • Laboratory and research data formatting
  • Ensuring consistent precision across calculations
Note: If a precision value is omitted, the number is simply converted to a string without special formatting.

Comparison of Formatting Methods

Method What It Controls Example Input Example Output
toFixed(2) Decimal places after point 123.456 "123.46"
toPrecision(3) Total significant digits 123.456 "123"
toExponential(2) Decimals in exponential 123.456 "1.23e+2"

Common Patterns

Currency Formatting:

// Format as currency with 2 decimal places
"$" + $price.toFixed(2)              // Returns: "$45.67"

Percentage Display:

// Format percentage with 1 decimal
($ratio * 100).toFixed(1) + "%"      // Returns: "85.5%"

Scientific Data:

// Format measurement with 3 significant figures
$measurement.toPrecision(3)          // Returns: "12.3"

Compact Large Numbers:

// Display large number in scientific notation
$population.toExponential(2)         // Returns: "7.89e+9"

Consistent Decimal Display:

// Ensure all prices show 2 decimals (including .00)
$prices.map(price => price.toFixed(2))

Conditional Formatting:

// Use exponential for very large/small, fixed otherwise
$value > 10000 || $value < 0.001 ?
  $value.toExponential(2) :
  $value.toFixed(2)

Best Practices

  • Return Type: All three methods return strings, not numbers. If you need to perform further calculations, parse the result back to a number.
  • Rounding: Both toFixed() and toPrecision() perform rounding. Be aware of potential rounding errors in financial calculations.
  • Range Limits: toFixed() accepts 0-20 digits, toPrecision() accepts 1-21. Values outside these ranges will cause errors.
  • Currency: For currency, toFixed(2) is the standard choice as it ensures consistent decimal places.
  • Scientific Data: Use toPrecision() when the number of significant figures matters more than decimal places.
  • Display vs Calculation: Use these methods for display purposes. For calculations, work with the numeric values directly.
  • Locale Considerations: These methods use dot (.) as decimal separator. For localized formatting, consider using toLocaleDateString() or custom formatting logic.