format money even when locale is not supported

This commit is contained in:
Martin Brodbeck 2018-08-02 13:06:37 +02:00
parent da4e223b16
commit 36643cf737
6 changed files with 44 additions and 43 deletions

View File

@ -1,4 +1,5 @@
#include "article.h"
#include "utils.h"
#include <iomanip>
#include <sstream>
@ -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_; }

View File

@ -1,5 +1,6 @@
#include "marketplace.h"
#include "database.h"
#include "utils.h"
#include <algorithm>
#include <fstream>
@ -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)

View File

@ -1,4 +1,5 @@
#include "sale.h"
#include "utils.h"
#include <numeric>
@ -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_; }

View File

@ -1,8 +1,9 @@
#include "seller.h"
#include "utils.h"
#include <iomanip>
#include <numeric>
#include <sstream>
#include <iomanip>
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<int>(getArticles().size()); }
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)

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