8
0
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:
robocop 2009-02-15 13:23:38 +00:00
parent 071bc4e3c0
commit 5e5c5741fa
2 changed files with 69 additions and 2 deletions

View File

@ -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

View File

@ -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