From e1fa0fc70ade67eff36721ead8c368ddc6b805c9 Mon Sep 17 00:00:00 2001 From: Jack Del Vecchio Date: Thu, 22 Aug 2024 18:44:30 +0000 Subject: [PATCH] Add scientific notation support to Decimal::setDecimal --- rtl/nbcd/nbcd.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/rtl/nbcd/nbcd.cpp b/rtl/nbcd/nbcd.cpp index 92081b27a37..229b4a68f91 100644 --- a/rtl/nbcd/nbcd.cpp +++ b/rtl/nbcd/nbcd.cpp @@ -951,7 +951,13 @@ void Decimal::setReal(double value) void Decimal::setString(size32_t length, const char * buffer) { const char * limit = buffer+length; - const char * cur = buffer; + const char * cur = limit; + int sciNotation = 0; + while ((cur > buffer) && (*cur != 'E')) + cur--; + if (cur != buffer) + sciNotation = atoi(std::string(cur+1, limit).c_str()); + cur = buffer; while ((cur < limit) && (*cur == ' ')) cur++; @@ -975,8 +981,8 @@ void Decimal::setString(size32_t length, const char * buffer) int idx; for (idx = 0; idx < (int)numDigits; idx++) - digits[zeroDigit+idx] = cur[-(idx+1)] - '0'; // careful - if idx is unsigned, -(idx+1) is not sign-extended and fails if int size is not pointer size - msb = zeroDigit+(numDigits-1); + digits[zeroDigit+idx+sciNotation] = cur[-(idx+1)] - '0'; // careful - if idx is unsigned, -(idx+1) is not sign-extended and fails if int size is not pointer size + msb = zeroDigit+(numDigits-1)+sciNotation; if ((cur < limit) && (*cur == '.')) { @@ -985,13 +991,13 @@ void Decimal::setString(size32_t length, const char * buffer) const char * start = cur; if (limit-cur > maxPrecision) limit = cur + maxPrecision; - byte * digit = digits + zeroDigit; + byte * digit = digits + zeroDigit + sciNotation; while ((cur < limit) && (isdigit(*cur))) *--digit = *cur++ - '0'; - lsb = zeroDigit - (cur-start); + lsb = zeroDigit - (cur-start) + sciNotation; } else - lsb = zeroDigit; + lsb = zeroDigit + sciNotation; }