added method & tests to the class NumberFormatter for bool values

This commit is contained in:
Marian Krivos 2009-03-23 20:10:51 +00:00
parent 764bbf2e1d
commit fd98b17390
3 changed files with 80 additions and 41 deletions

View File

@ -60,6 +60,13 @@ class Foundation_API NumberFormatter
/// formatting.
{
public:
enum BoolFormat
{
FMT_TRUE_FALSE,
FMT_YES_NO,
FMT_ON_OFF
};
static std::string format(int value);
/// Formats an integer value in decimal notation.
@ -216,6 +223,10 @@ public:
/// sixteen (64-bit architectures) characters wide
/// field in hexadecimal notation.
static std::string format(bool value, BoolFormat format = FMT_TRUE_FALSE);
/// Formats a bool value in decimal/text notation,
/// according to format parameter.
static void append(std::string& str, int value);
/// Formats an integer value in decimal notation.

View File

@ -51,6 +51,27 @@
namespace Poco {
std::string NumberFormatter::format(bool value, BoolFormat format)
{
switch(format)
{
default:
case FMT_TRUE_FALSE:
if (value == true)
return "true";
return "false";
case FMT_YES_NO:
if (value == true)
return "yes";
return "no";
case FMT_ON_OFF:
if (value == true)
return "on";
return "off";
}
}
void NumberFormatter::append(std::string& str, int value)
{
char buffer[64];

View File

@ -89,6 +89,13 @@ void NumberFormatterTest::testFormat()
assert (NumberFormatter::format(12.25) == "12.25");
assert (NumberFormatter::format(12.25, 4) == "12.2500");
assert (NumberFormatter::format(12.25, 8, 4) == " 12.2500");
assert (NumberFormatter::format(true, NumberFormatter::FMT_TRUE_FALSE) == "true");
assert (NumberFormatter::format(false, NumberFormatter::FMT_TRUE_FALSE) == "false");
assert (NumberFormatter::format(true, NumberFormatter::FMT_YES_NO) == "yes");
assert (NumberFormatter::format(false, NumberFormatter::FMT_YES_NO) == "no");
assert (NumberFormatter::format(true, NumberFormatter::FMT_ON_OFF) == "on");
assert (NumberFormatter::format(false, NumberFormatter::FMT_ON_OFF) == "off");
}