binance
Version:
Professional Node.js & JavaScript SDK for Binance REST APIs & WebSockets, with TypeScript & end-to-end tests.
41 lines • 1.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.roundToTickSize = roundToTickSize;
exports.roundToStepSize = roundToStepSize;
/**
* Rounds a price to the correct number of decimal places based on the symbol's tick size
*/
function roundToTickSize(price, tickSize) {
if (!tickSize) {
return price;
}
// Calculate precision from tick size (e.g., 0.00010000 → 4 decimal places)
const precision = tickSize.indexOf('1') - 1;
if (precision < 0) {
return Math.floor(price);
}
// Round to the nearest tick
const tickSizeNum = parseFloat(tickSize);
const rounded = Math.floor(price / tickSizeNum) * tickSizeNum;
// Format with correct precision
return +rounded.toFixed(precision);
}
/**
* Rounds a quantity to the correct step size
*/
function roundToStepSize(quantity, stepSize) {
if (!stepSize) {
return quantity;
}
// Calculate precision from step size
const precision = stepSize.indexOf('1') - 1;
if (precision < 0) {
return Math.floor(quantity);
}
// Round to the nearest step
const stepSizeNum = parseFloat(stepSize);
const rounded = Math.floor(quantity / stepSizeNum) * stepSizeNum;
// Format with correct precision
return +rounded.toFixed(precision);
}
//# sourceMappingURL=rounding.js.map