Skip to main content

roundValue

Round a number to a number of decimal places. The hub uses it for values it reports, such as a sound's state and volume, so you can round your own numbers the same way.

roundValue(value: number, decimals?: number): number;

Parameters​

ParameterTypeDefaultDescription
valuenumberrequiredThe number to round.
decimalsnumber2Decimal places to keep. A negative value is treated as 0.

Returns​

number: the rounded value. NaN stays NaN, and Infinity and -Infinity come back unchanged.

Example​

import { SoundHub } from 'soundhub';

const soundHub = new SoundHub();

soundHub.roundValue(3.14159); // 3.14
soundHub.roundValue(3.14159, 4); // 3.1416
soundHub.roundValue(2.5, 0); // 3
soundHub.roundValue(12.7, -1); // 13, a negative count is treated as 0

Good to know​

  • It multiplies, uses Math.round and divides again. Halves round up (-2.5 becomes -2), and floating point can nudge an edge case the other way: roundValue(1.005) gives 1, not 1.01.
  • Values from getSoundState, such as currentTime and progress, are rounded to 4 decimals with this method.

See also​