-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create number_format modifier plus a test
- Loading branch information
1 parent
2008a07
commit d8b916e
Showing
4 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* NumberFormat.h | ||
* | ||
* Built-in "|number_format" modifier | ||
* | ||
* @author David van Erkelens <[email protected]> | ||
* @copyright 2019 Copernica BV | ||
*/ | ||
|
||
/** | ||
* Namespace | ||
*/ | ||
namespace SmartTpl { namespace Internal { | ||
|
||
/** | ||
* Class definition | ||
*/ | ||
class NumberFormatModifier : public Modifier | ||
{ | ||
public: | ||
/** | ||
* Destructor | ||
*/ | ||
virtual ~NumberFormatModifier() {}; | ||
|
||
/** | ||
* Modify a value object | ||
* @param input | ||
* @param params Parameters used for this modification | ||
* @return Value | ||
*/ | ||
VariantValue modify(const Value &input, const SmartTpl::Parameters ¶ms) override | ||
{ | ||
// Convert input to double | ||
double original(input.toDouble()); | ||
|
||
// make sure we have a parameter containing the format | ||
if (params.size() < 1) throw NoModification(); | ||
|
||
// get the amound of decimals to output | ||
int decimals = params[0].toInteger(); | ||
|
||
// buffer to create the printf format | ||
char format[10]; | ||
|
||
// format the format | ||
sprintf(format, "%%.%if", decimals); | ||
|
||
// calculate size of new string | ||
size_t size = snprintf(nullptr, 0, format, original); | ||
|
||
// create buffer | ||
char buffer[size + 1]; | ||
|
||
// create new string | ||
sprintf(buffer, format, original); | ||
|
||
// create object | ||
return VariantValue(buffer); | ||
} | ||
}; | ||
|
||
/** | ||
* End namespace | ||
*/ | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters