Compare commits

...

2 commits

Author SHA1 Message Date
2d670f4e18 fix 2018-08-02 13:06:51 +02:00
36643cf737 format money even when locale is not supported 2018-08-02 13:06:37 +02:00
7 changed files with 46 additions and 44 deletions

View file

@ -14,7 +14,8 @@ set(CORE_SOURCES
sale.cpp sale.cpp
marketplace.cpp marketplace.cpp
excelreader.cpp excelreader.cpp
jsonutil jsonutil.cpp
utils.cpp
) )
add_library(core STATIC ${CORE_SOURCES}) add_library(core STATIC ${CORE_SOURCES})

View file

@ -1,4 +1,5 @@
#include "article.h" #include "article.h"
#include "utils.h"
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
@ -30,10 +31,7 @@ int Article::getPrice() const { return price_; }
std::string Article::getPriceAsString() const std::string Article::getPriceAsString() const
{ {
std::stringstream sumStream; return formatCentAsEuroString(price_);
// sumStream.imbue(std::locale("de_DE.utf8"));
sumStream << std::right << std::setw(10) << std::showbase << std::put_money(price_, false);
return sumStream.str();
} }
int Article::getArticleNo() const { return articleNo_; } int Article::getArticleNo() const { return articleNo_; }

View file

@ -1,5 +1,6 @@
#include "marketplace.h" #include "marketplace.h"
#include "database.h" #include "database.h"
#include "utils.h"
#include <algorithm> #include <algorithm>
#include <fstream> #include <fstream>
@ -117,14 +118,8 @@ int Marketplace::getBasketSumInCent()
std::string Marketplace::getBasketSumAsString() std::string Marketplace::getBasketSumAsString()
{ {
int sumInCent = getBasketSumInCent(); int sumInCent = getBasketSumInCent();
// double sumInEuro = sumInCent / 100.0L;
// std::stringstream sumStream; return formatCentAsEuroString(sumInCent);
// 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();
} }
void Marketplace::removeSale(boost::uuids::uuid uuid) void Marketplace::removeSale(boost::uuids::uuid uuid)
@ -165,9 +160,7 @@ int Marketplace::getOverallSumInCent()
std::string Marketplace::getOverallSumAsString() std::string Marketplace::getOverallSumAsString()
{ {
int sum = getOverallSumInCent(); int sum = getOverallSumInCent();
std::stringstream sumStream; return formatCentAsEuroString(sum);
sumStream << std::right << std::setw(10) << std::showbase << std::put_money(sum, false);
return sumStream.str();
} }
int Marketplace::getOverallPaymentInCent(int percent, int maxFee) 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) std::string Marketplace::getOverallPaymentAsString(int percent, int maxFee)
{ {
int sum = getOverallPaymentInCent(percent, maxFee); int sum = getOverallPaymentInCent(percent, maxFee);
std::stringstream sumStream; return formatCentAsEuroString(sum);
sumStream << std::right << std::setw(10) << std::showbase << std::put_money(sum, false);
return sumStream.str();
} }
std::string Marketplace::getOverallRevenueAsString(int percent, int maxFee) std::string Marketplace::getOverallRevenueAsString(int percent, int maxFee)
{ {
int sum = getOverallSumInCent(); int sum = getOverallSumInCent();
int pay = getOverallPaymentInCent(percent, maxFee); int pay = getOverallPaymentInCent(percent, maxFee);
std::stringstream sumStream; return formatCentAsEuroString(sum - pay);
sumStream << std::right << std::setw(10) << std::showbase << std::put_money(sum - pay, false);
return sumStream.str();
} }
double marketFee(int sum, int percent, int maxFee) 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::string marketFeeAsString(int sum, int percent, int maxFee)
{ {
std::stringstream feeStream; return formatCentAsEuroString(marketFee(sum, percent, maxFee));
feeStream << std::right << std::setw(10) << std::showbase
<< std::put_money(marketFee(sum, percent, maxFee), false);
return feeStream.str();
} }
std::string paymentAsString(int sumInCent, int percent, int maxFeeInCent) std::string paymentAsString(int sumInCent, int percent, int maxFeeInCent)
{ {
std::stringstream feeStream; return formatCentAsEuroString(sumInCent - marketFee(sumInCent, percent, maxFeeInCent));
feeStream << std::right << std::setw(10) << std::showbase
<< std::put_money(sumInCent - marketFee(sumInCent, percent, maxFeeInCent), false);
return feeStream.str();
} }
std::string escapeCsvValue(const std::string& value, const char delimiter) std::string escapeCsvValue(const std::string& value, const char delimiter)

View file

@ -1,4 +1,5 @@
#include "sale.h" #include "sale.h"
#include "utils.h"
#include <numeric> #include <numeric>
@ -31,12 +32,7 @@ int Sale::sumInCents()
return sum; return sum;
} }
std::string Sale::sumAsString() std::string Sale::sumAsString() { return formatCentAsEuroString(sumInCents()); }
{
std::stringstream sumStream;
sumStream << std::right << std::setw(10) << std::showbase << std::put_money(sumInCents(), false);
return sumStream.str();
}
std::string Sale::getTimestamp() { return timestamp_; } std::string Sale::getTimestamp() { return timestamp_; }

View file

@ -1,8 +1,9 @@
#include "seller.h" #include "seller.h"
#include "utils.h"
#include <iomanip>
#include <numeric> #include <numeric>
#include <sstream> #include <sstream>
#include <iomanip>
Seller::Seller(const std::string& firstName, const std::string& lastName, int sellerNo, Seller::Seller(const std::string& firstName, const std::string& lastName, int sellerNo,
int numArticlesOffered) int numArticlesOffered)
@ -91,14 +92,8 @@ int Seller::sumInCents()
return sum; return sum;
} }
std::string Seller::sumAsString() std::string Seller::sumAsString() { return formatCentAsEuroString(sumInCents()); }
{
std::stringstream sumStream;
sumStream << std::right << std::setw(10) << std::showbase << std::put_money(sumInCents(), false);
return sumStream.str();
}
// int Seller::numArticlesTotal() const { return static_cast<int>(getArticles().size()); }
bool operator<(const Seller& li, const Seller& re) { return li.sellerNo_ < re.sellerNo_; } bool operator<(const Seller& li, const Seller& re) { return li.sellerNo_ < re.sellerNo_; }
bool operator<(const std::unique_ptr<Seller>& li, const std::unique_ptr<Seller>& re) bool operator<(const std::unique_ptr<Seller>& li, const std::unique_ptr<Seller>& re)

21
src/core/utils.cpp Normal file
View file

@ -0,0 +1,21 @@
#include "utils.h"
#include <iomanip>
#include <numeric>
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();
}

8
src/core/utils.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef UTILS_H
#define UTILS_H
#include <string>
std::string formatCentAsEuroString(const int cent, int width = 10);
#endif