Commit 941cf247a9116fb7108a2289f8a9a1dc2b034c96
1 parent
b4b69b15
Add optional precision parameter to str() functions for floating point values.
The precision parameter allows setting the number of digits after the comma.
Showing
2 changed files
with
21 additions
and
5 deletions
include/GTools.hpp
| ... | ... | @@ -57,8 +57,8 @@ namespace gammalib { |
| 57 | 57 | std::string str(const int& value); |
| 58 | 58 | std::string str(const long int& value); |
| 59 | 59 | std::string str(const long long int& value); |
| 60 | - std::string str(const float& value); | |
| 61 | - std::string str(const double& value); | |
| 60 | + std::string str(const float& value, const int& precision = 0); | |
| 61 | + std::string str(const double& value, const int& precision = 0); | |
| 62 | 62 | char* tochar(const std::string& arg); |
| 63 | 63 | short toshort(const std::string& arg); |
| 64 | 64 | unsigned short toushort(const std::string& arg); | ... | ... |
src/support/GTools.cpp
| ... | ... | @@ -374,16 +374,24 @@ std::string gammalib::str(const long long int& value) |
| 374 | 374 | return s_value.str(); |
| 375 | 375 | } |
| 376 | 376 | |
| 377 | - | |
| 377 | + | |
| 378 | 378 | /***********************************************************************//** |
| 379 | 379 | * @brief Convert single precision value into string |
| 380 | 380 | * |
| 381 | 381 | * @param[in] value Single precision value to be converted into string. |
| 382 | + * @param[in] precision Floating point precision (optional). | |
| 382 | 383 | ***************************************************************************/ |
| 383 | -std::string gammalib::str(const float& value) | |
| 384 | +std::string gammalib::str(const float& value, const int& precision) | |
| 384 | 385 | { |
| 385 | 386 | std::ostringstream s_value; |
| 387 | + if (precision > 0) { | |
| 388 | + s_value.precision(precision); | |
| 389 | + s_value.setf(std::ios::fixed, std::ios::floatfield); | |
| 390 | + } | |
| 386 | 391 | s_value << value; |
| 392 | + if (precision > 0) { | |
| 393 | + s_value.unsetf(std::ios::floatfield); | |
| 394 | + } | |
| 387 | 395 | return s_value.str(); |
| 388 | 396 | } |
| 389 | 397 | |
| ... | ... | @@ -392,11 +400,19 @@ std::string gammalib::str(const float& value) |
| 392 | 400 | * @brief Convert double precision value into string |
| 393 | 401 | * |
| 394 | 402 | * @param[in] value Double precision value to be converted into string. |
| 403 | + * @param[in] precision Floating point precision (optional). | |
| 395 | 404 | ***************************************************************************/ |
| 396 | -std::string gammalib::str(const double& value) | |
| 405 | +std::string gammalib::str(const double& value, const int& precision) | |
| 397 | 406 | { |
| 398 | 407 | std::ostringstream s_value; |
| 408 | + if (precision > 0) { | |
| 409 | + s_value.precision(precision); | |
| 410 | + s_value.setf(std::ios::fixed, std::ios::floatfield); | |
| 411 | + } | |
| 399 | 412 | s_value << value; |
| 413 | + if (precision > 0) { | |
| 414 | + s_value.unsetf(std::ios::floatfield); | |
| 415 | + } | |
| 400 | 416 | return s_value.str(); |
| 401 | 417 | } |
| 402 | 418 | ... | ... |