Main
Vectors
Matrix
Matrix Manipulation
Transformation Matrices
Angles
Random
Bezier Curve
Equations
Path Movement
Color
Linear Interpolation
Derivatives
Collision Detection
Animation
Circle And Ellipse
Sequence
Combinatorics
Other

Set Decimal Places

This helper allows to format a number to show a selected number of decimal places.

import { setDecimalPlaces } from 'mz-math';

const res = setDecimalPlaces(1.2345, 2); // 1.23
const res = setDecimalPlaces(1.2399, 2); // 1.24
const res = setDecimalPlaces(1.2399, 0); // 1

The result of this function is a number (not a string), so sometimes fewer decimal places will be displayed after rounding:

const res = setDecimalPlaces(1.239999, 4); // 1.2400 = 1.24