- fixed GH #185: Poco::NumberFormatter::format(double value, int
precision) ignore precision == 0
This commit is contained in:
Aleksandar Fabijanic 2013-06-04 23:01:00 -05:00
parent ca16b76f9f
commit 0e6985c8a1
5 changed files with 49 additions and 15 deletions

View File

@ -73,6 +73,8 @@ Release 1.5.2 (2013-06-xx)
- fixed GH #209: Poco::NumberFormatter double length
- fixed GH #204: Upgrade zlib to 1.2.8
- fixed GH #198: The "application.configDir" property is not always created.
- fixed GH #185: Poco::NumberFormatter::format(double value, int precision)
ignore precision == 0
Release 1.5.1 (2013-01-11)
==========================

View File

@ -488,13 +488,14 @@ Foundation_API void floatToStr(char* buffer,
Foundation_API std::string& floatToStr(std::string& str,
float value,
int precision = 0,
int precision = -1,
int width = 0,
char thSep = 0,
char decSep = 0);
/// Converts a float value, assigns it to the supplied string and returns the reference.
/// This function calls floatToStr(char*, int, float, int, int) and formats the result according to
/// precision (total number of digits after the decimal point) and width (total length of formatted string).
/// precision (total number of digits after the decimal point, -1 means ignore precision argument)
/// and width (total length of formatted string).
Foundation_API void doubleToStr(char* buffer,
@ -510,13 +511,14 @@ Foundation_API void doubleToStr(char* buffer,
Foundation_API std::string& doubleToStr(std::string& str,
double value,
int precision = 0,
int precision = -1,
int width = 0,
char thSep = 0,
char decSep = 0);
/// Converts a double value, assigns it to the supplied string and returns the reference.
/// This function calls doubleToStr(char*, int, float, int, int) and formats the result according to
/// precision (total number of digits after the decimal point) and width (total length of formatted string).
/// precision (total number of digits after the decimal point, -1 means ignore precision argument)
/// and width (total length of formatted string).
Foundation_API float strToFloat(const char* str);

View File

@ -58,6 +58,10 @@ void pad(std::string& str, int precision, int width, char prefix = ' ', char dec
/// Alternative prefix (e.g. zero instead of space) can be supplied by caller.
/// Used only internally.
{
// these cases should never happen, if they do, it's a library bug
poco_assert_dbg (precision > 0);
poco_assert_dbg (str.length());
std::string::size_type decSepPos = str.find(decSep);
if (decSepPos == std::string::npos)
{
@ -66,11 +70,26 @@ void pad(std::string& str, int precision, int width, char prefix = ' ', char dec
}
std::string::size_type frac = str.length() - decSepPos - 1;
if (precision)
std::string::size_type ePos = str.find_first_of("eE");
std::auto_ptr<std::string> eStr;
if (ePos != std::string::npos)
{
if (frac < precision) str.append(precision - frac, '0');
else if (frac > precision) str = str.substr(0, frac + precision);
eStr.reset(new std::string(str.substr(ePos, std::string::npos)));
frac -= eStr->length();
str = str.substr(0, str.length() - eStr->length());
}
if (frac != precision)
{
if (frac < precision)
str.append(precision - frac, '0');
else if ((frac > precision) && (decSepPos != std::string::npos))
str = str.substr(0, decSepPos + 1 + precision);
}
if (eStr.get()) str += *eStr;
if (width && (str.length() < width)) str.insert(str.begin(), width - str.length(), prefix);
}
@ -136,6 +155,9 @@ void floatToStr(char* buffer, int bufferSize, float value, int lowDec, int highD
std::string& floatToStr(std::string& str, float value, int precision, int width, char thSep, char decSep)
{
if (!decSep) decSep = '.';
if (precision == 0) value = std::floor(value);
char buffer[POCO_MAX_FLT_STRING_LEN];
floatToStr(buffer, POCO_MAX_FLT_STRING_LEN, value);
str = buffer;
@ -144,7 +166,7 @@ std::string& floatToStr(std::string& str, float value, int precision, int width,
replaceInPlace(str, '.', decSep);
if (thSep) insertThousandSep(str, thSep, decSep);
if (precision || width) pad(str, precision, width, ' ', decSep ? decSep : '.');
if (precision > 0 || width) pad(str, precision, width, ' ', decSep ? decSep : '.');
return str;
}
@ -165,6 +187,7 @@ void doubleToStr(char* buffer, int bufferSize, double value, int lowDec, int hig
std::string& doubleToStr(std::string& str, double value, int precision, int width, char thSep, char decSep)
{
if (!decSep) decSep = '.';
if (precision == 0) value = std::floor(value);
char buffer[POCO_MAX_FLT_STRING_LEN];
doubleToStr(buffer, POCO_MAX_FLT_STRING_LEN, value);
@ -174,7 +197,7 @@ std::string& doubleToStr(std::string& str, double value, int precision, int widt
replaceInPlace(str, '.', decSep);
if (thSep) insertThousandSep(str, thSep, decSep);
if (precision || width) pad(str, precision, width, ' ', decSep ? decSep : '.');
if (precision > 0 || width) pad(str, precision, width, ' ', decSep ? decSep : '.');
return str;
}

View File

@ -233,8 +233,15 @@ void NumberFormatterTest::testFormatFloat()
assert(NumberFormatter::format(-12.25, 10, 4) == " -12.2500");
assert(NumberFormatter::format(-12.25, 10, 2) == " -12.25");
double dTest = 50.0;
assert (NumberFormatter::format(dTest, 3) == "50.000");
assert (NumberFormatter::format(50.0, 3) == "50.000");
assert (NumberFormatter::format(50.0f, 3) == "50.000");
assert (NumberFormatter::format(50.123, 3) == "50.123");
assert (NumberFormatter::format(50.123f, 3) == "50.123");
assert (NumberFormatter::format(50.123, 0) == "50");
assert (NumberFormatter::format(50.123f, 0) == "50");
assert (NumberFormatter::format(50.546, 0) == "50");
assert (NumberFormatter::format(50.546f, 0) == "50");
}

View File

@ -1000,11 +1000,11 @@ void StringTest::testFloatToString()
double val = 1.03721575516329e-112;
std::string str;
assert (doubleToStr(str, val, 15, 21) == "1.03721575516329e-112");
assert (doubleToStr(str, val, 15, 22) == " 1.03721575516329e-112");
assert (doubleToStr(str, val, 14, 21) == "1.03721575516329e-112");
assert (doubleToStr(str, val, 14, 22) == " 1.03721575516329e-112");
val = -val;
assert (doubleToStr(str, val, 15, 22) == "-1.03721575516329e-112");
assert (doubleToStr(str, val, 15, 23) == " -1.03721575516329e-112");
assert (doubleToStr(str, val, 14, 22) == "-1.03721575516329e-112");
assert (doubleToStr(str, val, 14, 23) == " -1.03721575516329e-112");
val = -10372157551632.9;
assert (doubleToStr(str, val, 1, 21, ',') == "-10,372,157,551,632.9");