История изменений
Исправление dataman, (текущая версия) :
А так выглядит форматирование в strf:
#include <strf/to_string.hpp>
#include <strf/locale.hpp> // strf::locale_numpunct
void numeric_punctuation()
{
// German punctuation
auto s = strf::to_string.with(strf::numpunct_de_DE) (!strf::fixed(1000000.5));
assert(s == "1.000.000,5");
// You have to use format function `strf::punct` or `.punct()`
// or operator! to apply punctuation
s = strf::to_string.with(strf::numpunct_de_DE)
( !strf::fixed(1000000.5), " ", strf::fixed(1000000.5)
, " ", strf::punct(10000), " ", 100000 );
assert(s == "1.000.000,5 1000000.5 10.000 100000");
// Extracting punctuation from locale
if (setlocale(LC_NUMERIC, "as_IN")) {
auto loc_punct = strf::locale_numpunct(); // provided by header <strf/locale.hpp>
auto s_loc = strf::to_string.with(loc_punct) (*!strf::fixed(1e+16));
assert(s_loc == "10,00,00,00,00,00,00,000.");
}
// Manually specifing a punctuation
constexpr auto my_fancy_punct = strf::numpunct<10>(3)
.thousands_sep(U'•')
.decimal_point(U'⎖'); // U+2396
auto s2 = strf::to_u8string.with(my_fancy_punct) (!strf::fixed(1000000.5));
assert(s2 == u8"1•000•000⎖5");
// With variable grouping
constexpr auto my_fancy_punct_2 = strf::numpunct<10>(3, 2, 1)
.thousands_sep(U'•')
.decimal_point(U'⎖');
auto s3 = strf::to_u8string.with(my_fancy_punct_2)
(strf::punct(10000000.125));
assert(s3 == u8"1•0•0•00•000⎖125");
// Non-decimal bases
constexpr auto my_hex_punct = strf::numpunct<16>(4).thousands_sep('\'');
auto s4 = strf::to_string.with(my_hex_punct)(!strf::hex(0xFFFFFFF));
assert(s4 == "fff'ffff");
}
void ranges()
{
int array[] = {10, 20, 30, 40};
// Basic
auto str = strf::to_string( "--[", strf::range(array), "]--");
assert(str == "--[10203040]--");
// With separator
str = strf::to_string( "--[", strf::separated_range(array, " / "), "]--");
assert(str == "--[10 / 20 / 30 / 40]--");
// With separator and formatting
str = strf::to_string( "--["
, *strf::hex(strf::separated_range(array, " / ")).p(4)
, "]--");
assert(str == "--[0x000a / 0x0014 / 0x001e / 0x0028]--");
// Transforming the elements
auto func = [](int x){ return strf::join('<', strf::pad0(x, 4), '>'); };
str = strf::to_string(strf::range(array, func));
assert(str == "<0010><0020><0030><0040>");
str = strf::to_string(strf::separated_range(array, " ", func));
assert(str == "<0010> <0020> <0030> <0040>");
}
Исходная версия dataman, :
А так выглядит форматирование в strf:
#include <strf/to_string.hpp>
#include <strf/locale.hpp> // strf::locale_numpunct
void numeric_punctuation()
{
// German punctuation
auto s = strf::to_string.with(strf::numpunct_de_DE) (!strf::fixed(1000000.5));
assert(s == "1.000.000,5");
// You have to use format function `strf::punct` or `.punct()`
// or operator! to apply punctuation
s = strf::to_string.with(strf::numpunct_de_DE)
( !strf::fixed(1000000.5), " ", strf::fixed(1000000.5)
, " ", strf::punct(10000), " ", 100000 );
assert(s == "1.000.000,5 1000000.5 10.000 100000");
// Extracting punctuation from locale
if (setlocale(LC_NUMERIC, "as_IN")) {
auto loc_punct = strf::locale_numpunct(); // provided by header <strf/locale.hpp>
auto s_loc = strf::to_string.with(loc_punct) (*!strf::fixed(1e+16));
assert(s_loc == "10,00,00,00,00,00,00,000.");
}
// Manually specifing a punctuation
constexpr auto my_fancy_punct = strf::numpunct<10>(3)
.thousands_sep(U'•')
.decimal_point(U'⎖'); // U+2396
auto s2 = strf::to_u8string.with(my_fancy_punct) (!strf::fixed(1000000.5));
assert(s2 == u8"1•000•000⎖5");
// With variable grouping
constexpr auto my_fancy_punct_2 = strf::numpunct<10>(3, 2, 1)
.thousands_sep(U'•')
.decimal_point(U'⎖');
auto s3 = strf::to_u8string.with(my_fancy_punct_2)
(strf::punct(10000000.125));
assert(s3 == u8"1•0•0•00•000⎖125");
// Non-decimal bases
constexpr auto my_hex_punct = strf::numpunct<16>(4).thousands_sep('\'');
auto s4 = strf::to_string.with(my_hex_punct)(!strf::hex(0xFFFFFFF));
assert(s4 == "fff'ffff");
}