Files
spark/src/lib_binance_execution/binance_execution/quantity.h
T

41 lines
1.1 KiB
C++

#pragma once
#include <charconv>
#include <string>
namespace binance_execution {
void quantity_to_string(
double value,
std::string &dest,
int n_decimals = 4,
std::size_t capacity = 24);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void quantity_to_string(
double value, std::string &dest, int n_decimals, std::size_t capacity) {
const std::size_t orig_sz = dest.size();
dest.resize(orig_sz + capacity);
char *buffer = &dest[orig_sz];
auto [ptr, ec] = std::to_chars(
buffer, buffer + capacity, value, std::chars_format::fixed, n_decimals);
if (ec != std::errc{}) {
// Formatting failed
dest.resize(orig_sz);
return;
}
// Resize to the actual formatted length
dest.resize(orig_sz + (ptr - buffer));
// Remove trailing zeros
dest.erase(dest.find_last_not_of('0') + 1);
// Remove trailing decimal point if necessary
if (!dest.empty() && dest.back() == '.') {
dest.pop_back();
}
}
} // namespace binance_execution