Poco::NumberFormatter::format(float, precision) rounding changed #1128

This commit is contained in:
Alex Fabijanic
2016-02-28 22:59:29 -06:00
parent 86657f38f4
commit 14a0872556
3 changed files with 62 additions and 1 deletions

View File

@@ -70,9 +70,46 @@ void pad(std::string& str, int precision, int width, char prefix = ' ', char dec
if (frac != precision) if (frac != precision)
{ {
if (frac < precision) if (frac < precision)
{
str.append(precision - frac, '0'); str.append(precision - frac, '0');
else if ((frac > precision) && (decSepPos != std::string::npos)) }
else if ((frac > precision) && (decSepPos != std::string::npos))
{
int pos = decSepPos + 1 + precision;
if (str[pos] >= '5') // we must round up
{
char carry = 0;
if(str[--pos] == '9')
{
str[pos] = '0';
carry = 1;
}
else
{
++str[pos];
carry = 0;
}
while (--pos >= 0)
{
if(str[pos] == decSep) continue;
if(carry)
{
if((str[pos] + carry) <= '9')
{
++str[pos];
carry = 0;
}
else
{
str[pos] = '0';
carry = 1;
}
}
}
if (carry) str.insert(str.begin(), 1, '1');
}
str = str.substr(0, decSepPos + 1 + precision); str = str.substr(0, decSepPos + 1 + precision);
}
} }
if (eStr.get()) str += *eStr; if (eStr.get()) str += *eStr;

View File

@@ -686,6 +686,28 @@ void StringTest::testStringToDouble()
} }
void StringTest::testNumericStringPadding()
{
std::string str;
assert (floatToStr(str, 0.999f, 2, 4) == "1.00");
assert (floatToStr(str, 0.945f, 2, 4) == "0.95");
assert (floatToStr(str, 0.944f, 2, 4) == "0.94");
assert (floatToStr(str, 12.45f, 2, 5) == "12.45");
assert (floatToStr(str, 12.45f, 1, 4) == "12.5");
assert (floatToStr(str, 12.45f, 2, 6) == " 12.45");
assert (floatToStr(str, 12.455f, 3, 7) == " 12.455");
assert (floatToStr(str, 12.455f, 2, 6) == " 12.46");
assert (doubleToStr(str, 0.999, 2, 4) == "1.00");
assert (doubleToStr(str, 0.945, 2, 4) == "0.95");
assert (doubleToStr(str, 0.944, 2, 4) == "0.94");
assert (doubleToStr(str, 12.45, 2, 5) == "12.45");
assert (doubleToStr(str, 12.45, 1, 4) == "12.5");
assert (doubleToStr(str, 12.45, 2, 6) == " 12.45");
assert (floatToStr(str, 12.455f, 3, 7) == " 12.455");
assert (floatToStr(str, 12.455f, 2, 6) == " 12.46");
}
void StringTest::testStringToFloatError() void StringTest::testStringToFloatError()
{ {
char ds = decimalSeparator(); char ds = decimalSeparator();
@@ -1166,6 +1188,7 @@ CppUnit::Test* StringTest::suite()
CppUnit_addTest(pSuite, StringTest, testStringToInt); CppUnit_addTest(pSuite, StringTest, testStringToInt);
CppUnit_addTest(pSuite, StringTest, testStringToFloat); CppUnit_addTest(pSuite, StringTest, testStringToFloat);
CppUnit_addTest(pSuite, StringTest, testStringToDouble); CppUnit_addTest(pSuite, StringTest, testStringToDouble);
CppUnit_addTest(pSuite, StringTest, testNumericStringPadding);
CppUnit_addTest(pSuite, StringTest, testStringToFloatError); CppUnit_addTest(pSuite, StringTest, testStringToFloatError);
CppUnit_addTest(pSuite, StringTest, testNumericLocale); CppUnit_addTest(pSuite, StringTest, testNumericLocale);
//CppUnit_addTest(pSuite, StringTest, benchmarkStrToFloat); //CppUnit_addTest(pSuite, StringTest, benchmarkStrToFloat);

View File

@@ -48,6 +48,7 @@ public:
void testStringToInt(); void testStringToInt();
void testStringToFloat(); void testStringToFloat();
void testStringToDouble(); void testStringToDouble();
void testNumericStringPadding();
void testStringToFloatError(); void testStringToFloatError();
void testNumericLocale(); void testNumericLocale();
void benchmarkStrToFloat(); void benchmarkStrToFloat();