mirror of
https://github.com/FirebirdSQL/firebird.git
synced 2025-01-23 18:03:04 +01:00
Commit this aux routine in case it's needed.
This commit is contained in:
parent
071bc4e3c0
commit
5e5c5741fa
@ -833,4 +833,69 @@ SINT64 query_performance_frequency()
|
||||
#endif
|
||||
}
|
||||
|
||||
void exactNumericToStr(SINT64 value, int scale, Firebird::string& target, bool append)
|
||||
{
|
||||
if (!value)
|
||||
{
|
||||
if (append)
|
||||
target.append("0", 1);
|
||||
else
|
||||
target.assign("0", 1);
|
||||
return;
|
||||
}
|
||||
|
||||
const int MAX_SCALE = 25;
|
||||
const int MAX_BUFFER = 50;
|
||||
|
||||
if (scale < -MAX_SCALE || scale > MAX_SCALE)
|
||||
return; // throw exception here?
|
||||
|
||||
const bool neg = value < 0;
|
||||
const bool dot = scale < 0; // Need the decimal separator or not?
|
||||
char buffer[MAX_BUFFER];
|
||||
int iter = MAX_BUFFER;
|
||||
buffer[--iter] = 0;
|
||||
if (scale > 0)
|
||||
{
|
||||
while (scale-- > 0)
|
||||
buffer[--iter] = '0';
|
||||
}
|
||||
bool dot_used = false;
|
||||
UINT64 uval = neg ? UINT64(-(value + 1)) + 1 : value; // avoid problems with MIN_SINT64
|
||||
while (uval)
|
||||
{
|
||||
buffer[--iter] = static_cast<char>(uval % 10) + '0';
|
||||
uval /= 10;
|
||||
if (dot && !++scale)
|
||||
{
|
||||
buffer[--iter] = '.';
|
||||
dot_used = true;
|
||||
}
|
||||
}
|
||||
if (dot)
|
||||
{
|
||||
// if scale > 0 we have N.M
|
||||
// if scale == 0 we have .M and we need 0.M
|
||||
// if scale < 0 we have pending zeroes and need 0.{0+}M
|
||||
if (!dot_used)
|
||||
{
|
||||
while (scale++ < 0)
|
||||
buffer[--iter] = '0';
|
||||
|
||||
buffer[--iter] = '.';
|
||||
buffer[--iter] = '0';
|
||||
}
|
||||
else if (!scale)
|
||||
buffer[--iter] = '0';
|
||||
}
|
||||
if (neg)
|
||||
buffer[--iter] = '-';
|
||||
|
||||
const size_t len = MAX_BUFFER - iter - 1;
|
||||
if (append)
|
||||
target.append(buffer + iter, len);
|
||||
else
|
||||
target.assign(buffer + iter, len);
|
||||
}
|
||||
|
||||
} // namespace fb_utils
|
||||
|
@ -126,5 +126,7 @@ namespace fb_utils
|
||||
SINT64 query_performance_frequency();
|
||||
} // namespace fb_utils
|
||||
|
||||
void exactNumericToStr(SINT64 value, int scale, Firebird::string& target, bool append = false);
|
||||
|
||||
#endif // INCLUDE_UTILS_PROTO_H
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user