Skip to main content
@spa-tools/utilities

shiftDecimalPlaces()

If you've ever worked with financial systems APIs, you may have experienced how extreme numeric precision is sometimes handled by the backend by storing integers for a float value, along with the number of decimal places to shift when needing to convert back to a float.

Should you encounter having to deal with this on the frontend, the shiftDecimalPlaces() will be a good friend by handling this logic for you.

Usage

import { shiftDecimalPlaces } from '@spa-tools/utilities';

const num1 = 1234567890123456;

const shifted1 = shiftDecimalPlaces(num1, 5);
console.log(shifted1);
// --> 12345678901.23456

const shifted2 = shiftDecimalPlaces(num1, 10);
console.log(shifted2);
// --> 123456.7890123456

const shifted3 = shiftDecimalPlaces(num1, 5, 3);
console.log(shifted3);
// --> 12345678901.235

Arguments

NameTypeRequired?DefaultDescription
wholeNumbernumberno0The integer number to convert to float
placesToShiftnumberno0The number of decimal places to shift
resultFloatPrecisionnumberno0The decimal places to round float to

Returns

Returns a number.