Player12
An open source football management simulator
Loading...
Searching...
No Matches
number_format.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2// Football Management Project
3// Copyright (c) 2025 - 2026 Flavio Milinanni. All Rights Reserved.
4//
5// This file is part of the Football Management Project.
6// See the LICENSE file in the project root.
7// -----------------------------------------------------------------------------
8
9#pragma once
10
11#include <cstdint>
12#include <optional>
13#include <string>
14#include <string_view>
15
24namespace NumberFormat
25{
26
28struct Locale
29{
30 std::string decimal_mark = ".";
31 std::string group_mark = ",";
33 std::string money_pattern = "€{0}";
34 std::string thousand_unit = "K";
35 std::string million_unit = "M";
36 std::string billion_unit = "B";
37
39 static Locale english() { return {}; }
40};
41
43const Locale& current();
44
46std::string grouped(int64_t value, const Locale& locale = current());
47
49std::string decimal(double value, int digits, const Locale& locale = current());
50
52std::string money(int64_t amount, const Locale& locale = current());
53
55std::string moneyFull(int64_t amount, const Locale& locale = current());
56
58enum class MoneyError : uint8_t
59{
60 NONE,
61 EMPTY,
62 INVALID,
63 AMBIGUOUS,
64 TOO_LARGE,
65};
66
69{
70 std::optional<int64_t> value;
72};
73
85MoneyParse parseMoney(std::string_view text, const Locale& locale = current());
86
88const char* errorKey(MoneyError error);
89
90} // namespace NumberFormat
Numbers and money in the conventions of the current language.
Definition number_format.cpp:194
std::string moneyFull(int64_t amount, const Locale &locale)
Full money, e.g. "€1,250,000" / "€ 1.250.000".
Definition number_format.cpp:232
const Locale & current()
Conventions of the language loaded now.
Definition number_format.cpp:196
MoneyParse parseMoney(std::string_view text, const Locale &locale)
Reads a typed amount: "1,5M", "1.5M", "1.500.000", "1,500,000", "1500k", "€ 2,25 mln",...
Definition number_format.cpp:393
const char * errorKey(MoneyError error)
Language key explaining a parse error (nullptr for NONE).
Definition number_format.cpp:425
std::string grouped(int64_t value, const Locale &locale)
Integer with grouping marks, e.g. "38,500" / "38.500".
Definition number_format.cpp:198
MoneyError
Why a money text was refused.
Definition number_format.h:59
std::string money(int64_t amount, const Locale &locale)
Compact money, e.g. "€12.4M", "€850K", "-€3.1M", "€ 1,5 mln".
Definition number_format.cpp:213
std::string decimal(double value, int digits, const Locale &locale)
Fixed decimals with the decimal mark, e.g. "7.5" / "7,5".
Definition number_format.cpp:204
Separators and money units of one language.
Definition number_format.h:29
static Locale english()
The English conventions (also the defaults above).
Definition number_format.h:39
std::string billion_unit
Definition number_format.h:36
std::string group_mark
Definition number_format.h:31
std::string money_pattern
Definition number_format.h:33
std::string decimal_mark
Definition number_format.h:30
std::string million_unit
Definition number_format.h:35
std::string thousand_unit
Definition number_format.h:34
Result of parseMoney(): a value, or the reason it has none.
Definition number_format.h:69
MoneyError error
Definition number_format.h:71
std::optional< int64_t > value
Definition number_format.h:70