mirror of
https://github.com/pocoproject/poco.git
synced 2025-07-06 02:11:38 +02:00
added method & tests to the class NumberFormatter for bool values
This commit is contained in:
parent
764bbf2e1d
commit
fd98b17390
@ -60,6 +60,13 @@ class Foundation_API NumberFormatter
|
|||||||
/// formatting.
|
/// formatting.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
enum BoolFormat
|
||||||
|
{
|
||||||
|
FMT_TRUE_FALSE,
|
||||||
|
FMT_YES_NO,
|
||||||
|
FMT_ON_OFF
|
||||||
|
};
|
||||||
|
|
||||||
static std::string format(int value);
|
static std::string format(int value);
|
||||||
/// Formats an integer value in decimal notation.
|
/// Formats an integer value in decimal notation.
|
||||||
|
|
||||||
@ -216,6 +223,10 @@ public:
|
|||||||
/// sixteen (64-bit architectures) characters wide
|
/// sixteen (64-bit architectures) characters wide
|
||||||
/// field in hexadecimal notation.
|
/// 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);
|
static void append(std::string& str, int value);
|
||||||
/// Formats an integer value in decimal notation.
|
/// Formats an integer value in decimal notation.
|
||||||
|
|
||||||
|
@ -51,6 +51,27 @@
|
|||||||
namespace Poco {
|
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)
|
void NumberFormatter::append(std::string& str, int value)
|
||||||
{
|
{
|
||||||
char buffer[64];
|
char buffer[64];
|
||||||
|
@ -89,6 +89,13 @@ void NumberFormatterTest::testFormat()
|
|||||||
assert (NumberFormatter::format(12.25) == "12.25");
|
assert (NumberFormatter::format(12.25) == "12.25");
|
||||||
assert (NumberFormatter::format(12.25, 4) == "12.2500");
|
assert (NumberFormatter::format(12.25, 4) == "12.2500");
|
||||||
assert (NumberFormatter::format(12.25, 8, 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");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user