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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | required | The number to round. |
decimals | number | 2 | Decimal 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.roundand divides again. Halves round up (-2.5becomes-2), and floating point can nudge an edge case the other way:roundValue(1.005)gives1, not1.01. - Values from
getSoundState, such ascurrentTimeandprogress, are rounded to 4 decimals with this method.
See also
getSoundState: the rounded state values of a sound.setSoundVolume: stores the volume it reports rounded to 2 decimals.