Skip to content

NumberDisplay

NumberDisplay (from scenerystack/scenery-phet) shows a Property<number | null>'s value as formatted text inside a bordered background rectangle. It pre-computes its width from a supplied displayRange so the background doesn't resize as the value changes — this is the readout half of NumberControl, which composes one internally, but it's equally useful standalone whenever you need a numeric readout without a slider attached.

ts
import { NumberDisplay } from 'scenerystack/scenery-phet';
import { NumberProperty } from 'scenerystack/axon';
import { Range } from 'scenerystack/dot';

A minimal example

ts
const temperatureProperty = new NumberProperty( 20, { range: new Range( -20, 100 ) } );

const temperatureDisplay = new NumberDisplay( temperatureProperty, new Range( -20, 100 ), {
  decimalPlaces: 1,
  valuePattern: '{{value}} °C',
  align: 'right'
} );

Constructor

ts
new NumberDisplay(
  numberProperty: TReadOnlyProperty<number | null>,
  displayRange: Range,
  providedOptions?: NumberDisplayOptions
)

displayRange is used only to measure the widest possible formatted string (so the background is sized once and doesn't jitter) — it's independent of numberProperty's own range and doesn't clamp the displayed value.

Options

OptionDefaultEffect
align'right'Horizontal alignment of the value text ('center', 'left', 'right')
valuePatternSunConstants.VALUE_NAMED_PLACEHOLDER ('')Template the formatted value is filled into, e.g. ' m/s'
decimalPlaces0Decimal places to show; null displays the full value. Mutually exclusive with numberFormatter
numberFormatternullA ( n: number ) => string for full control over formatting; mutually exclusive with valuePattern/decimalPlaces/numberFormatOptions
useRichTextfalseRender the value with RichText instead of Text (needed for markup, e.g. superscripts)
textOptions{ font: new PhetFont(20), fill: 'black' }Options forwarded to the internal Text/RichText
xMargin / yMargin8 / 2Margin between the value text and the background edge
cornerRadius0Background rectangle corner radius
backgroundFill / backgroundStroke'white' / 'lightGray'Background rectangle colors
minBackgroundWidth0Floor on the computed background width
noValueStringMathSymbols.NO_VALUE (an em dash)Text shown when numberProperty.value is null — the PhET standard; avoid overriding lightly
noValueAlign / noValuePatternfall back to align / valuePatternSeparate alignment/pattern for the no-value case, if needed

Public API

MemberDescription
valueStringPropertyRead-only TReadOnlyProperty<string> of the currently displayed (visual) text
numberFont / numberFillSettable properties for the value text's font and fill
backgroundFill / backgroundStroke / backgroundWidthSettable properties for the background rectangle

valuePattern and decimalPlaces are mutually exclusive with numberFormatter

Pass either the simple valuePattern + decimalPlaces pair, or a numberFormatter function for full control — never both. NumberDisplay asserts if it detects both were customized, since it can't tell which formatting path should win.