From 36643cf7370c481d22f3cfb3d4243a21755621a0 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Thu, 2 Aug 2018 13:06:37 +0200 Subject: [PATCH] format money even when locale is not supported --- src/core/article.cpp | 6 ++---- src/core/marketplace.cpp | 33 ++++++++------------------------- src/core/sale.cpp | 8 ++------ src/core/seller.cpp | 11 +++-------- src/core/utils.cpp | 21 +++++++++++++++++++++ src/core/utils.h | 8 ++++++++ 6 files changed, 44 insertions(+), 43 deletions(-) create mode 100644 src/core/utils.cpp create mode 100644 src/core/utils.h diff --git a/src/core/article.cpp b/src/core/article.cpp index c4fdce7..0715d93 100644 --- a/src/core/article.cpp +++ b/src/core/article.cpp @@ -1,4 +1,5 @@ #include "article.h" +#include "utils.h" #include #include @@ -30,10 +31,7 @@ int Article::getPrice() const { return price_; } std::string Article::getPriceAsString() const { - std::stringstream sumStream; - // sumStream.imbue(std::locale("de_DE.utf8")); - sumStream << std::right << std::setw(10) << std::showbase << std::put_money(price_, false); - return sumStream.str(); + return formatCentAsEuroString(price_); } int Article::getArticleNo() const { return articleNo_; } diff --git a/src/core/marketplace.cpp b/src/core/marketplace.cpp index 90dc0fa..19712e3 100644 --- a/src/core/marketplace.cpp +++ b/src/core/marketplace.cpp @@ -1,5 +1,6 @@ #include "marketplace.h" #include "database.h" +#include "utils.h" #include #include @@ -117,14 +118,8 @@ int Marketplace::getBasketSumInCent() std::string Marketplace::getBasketSumAsString() { int sumInCent = getBasketSumInCent(); - // double sumInEuro = sumInCent / 100.0L; - // std::stringstream sumStream; - // sumStream << std::fixed << std::setprecision(2) << sumInEuro << " €"; - // return sumStream.str(); - std::stringstream sumStream; - // sumStream.imbue(std::locale("de_DE.utf8")); - sumStream << std::right << std::setw(10) << std::showbase << std::put_money(sumInCent, false); - return sumStream.str(); + + return formatCentAsEuroString(sumInCent); } void Marketplace::removeSale(boost::uuids::uuid uuid) @@ -165,9 +160,7 @@ int Marketplace::getOverallSumInCent() std::string Marketplace::getOverallSumAsString() { int sum = getOverallSumInCent(); - std::stringstream sumStream; - sumStream << std::right << std::setw(10) << std::showbase << std::put_money(sum, false); - return sumStream.str(); + return formatCentAsEuroString(sum); } int Marketplace::getOverallPaymentInCent(int percent, int maxFee) @@ -182,18 +175,14 @@ int Marketplace::getOverallPaymentInCent(int percent, int maxFee) std::string Marketplace::getOverallPaymentAsString(int percent, int maxFee) { int sum = getOverallPaymentInCent(percent, maxFee); - std::stringstream sumStream; - sumStream << std::right << std::setw(10) << std::showbase << std::put_money(sum, false); - return sumStream.str(); + return formatCentAsEuroString(sum); } std::string Marketplace::getOverallRevenueAsString(int percent, int maxFee) { int sum = getOverallSumInCent(); int pay = getOverallPaymentInCent(percent, maxFee); - std::stringstream sumStream; - sumStream << std::right << std::setw(10) << std::showbase << std::put_money(sum - pay, false); - return sumStream.str(); + return formatCentAsEuroString(sum - pay); } double marketFee(int sum, int percent, int maxFee) @@ -207,18 +196,12 @@ double marketFee(int sum, int percent, int maxFee) std::string marketFeeAsString(int sum, int percent, int maxFee) { - std::stringstream feeStream; - feeStream << std::right << std::setw(10) << std::showbase - << std::put_money(marketFee(sum, percent, maxFee), false); - return feeStream.str(); + return formatCentAsEuroString(marketFee(sum, percent, maxFee)); } std::string paymentAsString(int sumInCent, int percent, int maxFeeInCent) { - std::stringstream feeStream; - feeStream << std::right << std::setw(10) << std::showbase - << std::put_money(sumInCent - marketFee(sumInCent, percent, maxFeeInCent), false); - return feeStream.str(); + return formatCentAsEuroString(sumInCent - marketFee(sumInCent, percent, maxFeeInCent)); } std::string escapeCsvValue(const std::string& value, const char delimiter) diff --git a/src/core/sale.cpp b/src/core/sale.cpp index 95bf4dc..30f53b3 100644 --- a/src/core/sale.cpp +++ b/src/core/sale.cpp @@ -1,4 +1,5 @@ #include "sale.h" +#include "utils.h" #include @@ -31,12 +32,7 @@ int Sale::sumInCents() return sum; } -std::string Sale::sumAsString() -{ - std::stringstream sumStream; - sumStream << std::right << std::setw(10) << std::showbase << std::put_money(sumInCents(), false); - return sumStream.str(); -} +std::string Sale::sumAsString() { return formatCentAsEuroString(sumInCents()); } std::string Sale::getTimestamp() { return timestamp_; } diff --git a/src/core/seller.cpp b/src/core/seller.cpp index 89f5aa8..31780e9 100644 --- a/src/core/seller.cpp +++ b/src/core/seller.cpp @@ -1,8 +1,9 @@ #include "seller.h" +#include "utils.h" +#include #include #include -#include Seller::Seller(const std::string& firstName, const std::string& lastName, int sellerNo, int numArticlesOffered) @@ -91,14 +92,8 @@ int Seller::sumInCents() return sum; } -std::string Seller::sumAsString() -{ - std::stringstream sumStream; - sumStream << std::right << std::setw(10) << std::showbase << std::put_money(sumInCents(), false); - return sumStream.str(); -} +std::string Seller::sumAsString() { return formatCentAsEuroString(sumInCents()); } -// int Seller::numArticlesTotal() const { return static_cast(getArticles().size()); } bool operator<(const Seller& li, const Seller& re) { return li.sellerNo_ < re.sellerNo_; } bool operator<(const std::unique_ptr& li, const std::unique_ptr& re) diff --git a/src/core/utils.cpp b/src/core/utils.cpp new file mode 100644 index 0000000..d0d91d3 --- /dev/null +++ b/src/core/utils.cpp @@ -0,0 +1,21 @@ +#include "utils.h" + +#include +#include + +std::string formatCentAsEuroString(const int cent, int width) +{ + std::stringstream currStream; + + try { + std::locale myLocale("de_DE.utf8"); + currStream.imbue(myLocale); + currStream << std::right << std::setw(width) << std::showbase + << std::put_money(cent, false); + } catch (std::runtime_error& err) { + currStream << std::fixed << std::setw(width) << std::setprecision(2) << cent / 100.0L + << " €"; + } + + return currStream.str(); +} \ No newline at end of file diff --git a/src/core/utils.h b/src/core/utils.h new file mode 100644 index 0000000..a2adb20 --- /dev/null +++ b/src/core/utils.h @@ -0,0 +1,8 @@ +#ifndef UTILS_H +#define UTILS_H + +#include + +std::string formatCentAsEuroString(const int cent, int width = 10); + +#endif \ No newline at end of file