Javascript functions for numeric input and output

As a part of a calculator for model railroad stub switches, I developed a couple of javascript routines for input and output that allow more flexibility than the standard implementation.

This function converts to text for display from a numeric value, with a variable number of decimals, removing extra trailing zeroes. It also converts the (default inch) measure to millimeters based on the units global variable.

This function accepts text from an input box in various formats and units of measurement. The return value is always converted into inches.
Value formats:

  • Decimal: a value such as 1.5
  • Mixed fraction: a value such as 1 1/2
  • Fraction: a value such as 3/8
  • Decimal: a value such as 0.375

Measure specifiers:

  • in: inches
  • ft: feet
  • mm: millimeter
  • cm: centimeter

Comments are closed.