mirror of
https://github.com/pocoproject/poco.git
synced 2025-07-04 01:27:11 +02:00
fixed GH #185
- fixed GH #185: Poco::NumberFormatter::format(double value, int precision) ignore precision == 0
This commit is contained in:
parent
ca16b76f9f
commit
0e6985c8a1
@ -73,6 +73,8 @@ Release 1.5.2 (2013-06-xx)
|
|||||||
- fixed GH #209: Poco::NumberFormatter double length
|
- fixed GH #209: Poco::NumberFormatter double length
|
||||||
- fixed GH #204: Upgrade zlib to 1.2.8
|
- fixed GH #204: Upgrade zlib to 1.2.8
|
||||||
- fixed GH #198: The "application.configDir" property is not always created.
|
- 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)
|
Release 1.5.1 (2013-01-11)
|
||||||
==========================
|
==========================
|
||||||
|
@ -488,13 +488,14 @@ Foundation_API void floatToStr(char* buffer,
|
|||||||
|
|
||||||
Foundation_API std::string& floatToStr(std::string& str,
|
Foundation_API std::string& floatToStr(std::string& str,
|
||||||
float value,
|
float value,
|
||||||
int precision = 0,
|
int precision = -1,
|
||||||
int width = 0,
|
int width = 0,
|
||||||
char thSep = 0,
|
char thSep = 0,
|
||||||
char decSep = 0);
|
char decSep = 0);
|
||||||
/// Converts a float value, assigns it to the supplied string and returns the reference.
|
/// 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
|
/// 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,
|
Foundation_API void doubleToStr(char* buffer,
|
||||||
@ -510,13 +511,14 @@ Foundation_API void doubleToStr(char* buffer,
|
|||||||
|
|
||||||
Foundation_API std::string& doubleToStr(std::string& str,
|
Foundation_API std::string& doubleToStr(std::string& str,
|
||||||
double value,
|
double value,
|
||||||
int precision = 0,
|
int precision = -1,
|
||||||
int width = 0,
|
int width = 0,
|
||||||
char thSep = 0,
|
char thSep = 0,
|
||||||
char decSep = 0);
|
char decSep = 0);
|
||||||
/// Converts a double value, assigns it to the supplied string and returns the reference.
|
/// 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
|
/// 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);
|
Foundation_API float strToFloat(const char* str);
|
||||||
|
@ -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.
|
/// Alternative prefix (e.g. zero instead of space) can be supplied by caller.
|
||||||
/// Used only internally.
|
/// 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);
|
std::string::size_type decSepPos = str.find(decSep);
|
||||||
if (decSepPos == std::string::npos)
|
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;
|
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');
|
eStr.reset(new std::string(str.substr(ePos, std::string::npos)));
|
||||||
else if (frac > precision) str = str.substr(0, frac + precision);
|
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);
|
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)
|
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];
|
char buffer[POCO_MAX_FLT_STRING_LEN];
|
||||||
floatToStr(buffer, POCO_MAX_FLT_STRING_LEN, value);
|
floatToStr(buffer, POCO_MAX_FLT_STRING_LEN, value);
|
||||||
str = buffer;
|
str = buffer;
|
||||||
@ -144,7 +166,7 @@ std::string& floatToStr(std::string& str, float value, int precision, int width,
|
|||||||
replaceInPlace(str, '.', decSep);
|
replaceInPlace(str, '.', decSep);
|
||||||
|
|
||||||
if (thSep) insertThousandSep(str, thSep, 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;
|
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)
|
std::string& doubleToStr(std::string& str, double value, int precision, int width, char thSep, char decSep)
|
||||||
{
|
{
|
||||||
if (!decSep) decSep = '.';
|
if (!decSep) decSep = '.';
|
||||||
|
if (precision == 0) value = std::floor(value);
|
||||||
|
|
||||||
char buffer[POCO_MAX_FLT_STRING_LEN];
|
char buffer[POCO_MAX_FLT_STRING_LEN];
|
||||||
doubleToStr(buffer, POCO_MAX_FLT_STRING_LEN, value);
|
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);
|
replaceInPlace(str, '.', decSep);
|
||||||
|
|
||||||
if (thSep) insertThousandSep(str, thSep, 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;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,8 +233,15 @@ void NumberFormatterTest::testFormatFloat()
|
|||||||
assert(NumberFormatter::format(-12.25, 10, 4) == " -12.2500");
|
assert(NumberFormatter::format(-12.25, 10, 4) == " -12.2500");
|
||||||
assert(NumberFormatter::format(-12.25, 10, 2) == " -12.25");
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1000,11 +1000,11 @@ void StringTest::testFloatToString()
|
|||||||
double val = 1.03721575516329e-112;
|
double val = 1.03721575516329e-112;
|
||||||
std::string str;
|
std::string str;
|
||||||
|
|
||||||
assert (doubleToStr(str, val, 15, 21) == "1.03721575516329e-112");
|
assert (doubleToStr(str, val, 14, 21) == "1.03721575516329e-112");
|
||||||
assert (doubleToStr(str, val, 15, 22) == " 1.03721575516329e-112");
|
assert (doubleToStr(str, val, 14, 22) == " 1.03721575516329e-112");
|
||||||
val = -val;
|
val = -val;
|
||||||
assert (doubleToStr(str, val, 15, 22) == "-1.03721575516329e-112");
|
assert (doubleToStr(str, val, 14, 22) == "-1.03721575516329e-112");
|
||||||
assert (doubleToStr(str, val, 15, 23) == " -1.03721575516329e-112");
|
assert (doubleToStr(str, val, 14, 23) == " -1.03721575516329e-112");
|
||||||
|
|
||||||
val = -10372157551632.9;
|
val = -10372157551632.9;
|
||||||
assert (doubleToStr(str, val, 1, 21, ',') == "-10,372,157,551,632.9");
|
assert (doubleToStr(str, val, 1, 21, ',') == "-10,372,157,551,632.9");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user