Using fmt for currency

This commit is contained in:
Martin Brodbeck 2022-07-07 16:53:44 +02:00
parent 6944051c31
commit 2b6628bdf8
3 changed files with 17 additions and 5 deletions

View file

@ -7,6 +7,8 @@ find_package(SQLite3 REQUIRED)
set(THREADS_PREFER_PTHREAD_FLAG ON) set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
find_package(fmt)
if (MINGW) if (MINGW)
find_package(XLNT REQUIRED STATIC) find_package(XLNT REQUIRED STATIC)
@ -34,10 +36,10 @@ set(CORE_SOURCES
add_library(core STATIC ${CORE_SOURCES}) add_library(core STATIC ${CORE_SOURCES})
target_include_directories(core PRIVATE ${PROJECT_SOURCE_DIR}/subprojects/csv-parser/single_include) target_include_directories(core PRIVATE ${PROJECT_SOURCE_DIR}/subprojects/csv-parser/single_include)
if (WIN32) if (WIN32)
target_link_libraries(core PRIVATE Boost::boost Boost::date_time sqlite3 nlohmann_json::nlohmann_json ${XLNT_LIBRARY} Threads::Threads) target_link_libraries(core PRIVATE Boost::boost Boost::date_time sqlite3 nlohmann_json::nlohmann_json ${XLNT_LIBRARY} Threads::Threads fmt::fmt)
target_link_libraries(core PRIVATE bcrypt) target_link_libraries(core PRIVATE bcrypt)
else() else()
target_link_libraries(core PRIVATE Boost::boost Boost::date_time sqlite3 nlohmann_json::nlohmann_json ${XLNT_LIBRARIES} Threads::Threads) target_link_libraries(core PRIVATE Boost::boost Boost::date_time sqlite3 nlohmann_json::nlohmann_json ${XLNT_LIBRARIES} Threads::Threads fmt::fmt)
endif() endif()
target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..) target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..)

View file

@ -1,16 +1,23 @@
#include "utils.h" #include "utils.h"
#include <algorithm> #include <algorithm>
#include <clocale>
#include <fmt/format.h>
#include <iomanip> #include <iomanip>
#include <numeric> #include <numeric>
#include <iostream>
using namespace fmt;
std::string formatCentAsEuroString(const int cent, int width) std::string formatCentAsEuroString(const int cent, int width)
{ {
std::stringstream currStream; /*std::stringstream currStream;
try { try {
std::locale myLocale("de_DE.utf8"); std::locale myLocale("de_DE.utf8");
currStream.imbue(myLocale); currStream.imbue(myLocale);
std::cout << ">>> " << fmt::format(myLocale, "{:6.2Lf}", 1.12345) << '\n';
currStream << std::right << std::setw(width) << std::showbase currStream << std::right << std::setw(width) << std::showbase
<< std::put_money(cent, false); << std::put_money(cent, false);
} catch (std::runtime_error &err) { } catch (std::runtime_error &err) {
@ -18,7 +25,10 @@ std::string formatCentAsEuroString(const int cent, int width)
<< std::setprecision(2) << cent / 100.0L << ""; << std::setprecision(2) << cent / 100.0L << "";
} }
return currStream.str(); return currStream.str();*/
std::locale myLocale{"de_DE.utf8"};
return fmt::format(myLocale, "{:{}.2Lf} €", cent / 100.0L, width);
} }
std::string &ltrim(std::string &str, const std::string &chars) std::string &ltrim(std::string &str, const std::string &chars)

View file

@ -5,7 +5,7 @@
#include <optional> #include <optional>
#include <string> #include <string>
std::string formatCentAsEuroString(const int cent, int width = 10); std::string formatCentAsEuroString(const int cent, int width = 6);
std::string &ltrim(std::string &str, const std::string &chars = "\t\n\v\f\r "); std::string &ltrim(std::string &str, const std::string &chars = "\t\n\v\f\r ");
std::string &rtrim(std::string &str, const std::string &chars = "\t\n\v\f\r "); std::string &rtrim(std::string &str, const std::string &chars = "\t\n\v\f\r ");
std::string &trim(std::string &str, const std::string &chars = "\t\n\v\f\r "); std::string &trim(std::string &str, const std::string &chars = "\t\n\v\f\r ");