- Show minus sign correctly for negative amounts "-$10"
- Have a thousand separator
- Have an optional argument to remove fractional part
- Allow decimal sign, thousand separator, and number of digits in a group to be configurable, by optional arguments
I haven't thought this through fully, but perhaps something similar to:
Code: Select all
local l = Lang.GetResource("core")
Format.Money = function (money, show_fraction)
local show_fraction_ = show_fraction or true;
somefunction(money, show_fraction_, l.DECIMAL, l.THOUSAND_SEPERATOR, l.GROUPING)
end
Code: Select all
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <locale>
std::string format_money(long int money,
bool show_fraction=true, char decimal=',',
char thousand=' ', std::string group="\3")
{
struct mymoney : std::moneypunct<char> {
private:
char decimal_point_; // fraction delimiter symbol
char thousand_sep_; // seperator character for thousands
int frac_digits_; // where to put decimal sign
std::string grouping_; // number of digits in a group
public:
mymoney(char decimal_point, char thousand_sep,
std::string grouping, int frac_digits){
this->decimal_point_ = decimal_point;
this->thousand_sep_ = thousand_sep;
this->frac_digits_ = frac_digits;
this->grouping_ = grouping;
}
char do_decimal_point() const {
return decimal_point_;
}
char do_thousands_sep() const {
return thousand_sep_;
}
int do_frac_digits() const {
return frac_digits_;
}
std::string do_grouping() const {
return grouping_;
}
std::string do_curr_symbol() const {
return "$";
}
std::string do_negative_sign() const {
return "-";
}
pattern do_neg_format() const{
return { {sign, symbol, value} };
}
};
if(!show_fraction && money % 100)
std::cerr << "Warning: Rounding off amount passed: " << money << std::endl;
double long Money = show_fraction ? money : money*0.01;
int frac_digits = show_fraction ? 2 : 0;
std::locale loc(std::locale(), new mymoney(decimal, thousand,
group, frac_digits));
std::ostringstream oss;
oss.imbue(loc);
oss << std::showbase << std::put_money(Money);
return oss.str();
}
int main(int argc, char **argv) {
long double money = -130000012;
std::cout << "number:\t" << money << std::endl;
std::cout << "money:\t" << format_money(money) << std::endl;
std::cout << "money:\t" << format_money(money,false) << std::endl;
std::cout << "money:\t" << format_money(money,true, '#') << std::endl;
return 0;
}