1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
static import std.metastrings;

struct None { enum Symbol = "",  Base = 1;    }
struct Kilo { enum Symbol = "k", Base = 1e+3; }
struct Mega { enum Symbol = "M", Base = 1e+6; }
struct Giga { enum Symbol = "G", Base = 1e+9; }
struct Tera { enum Symbol = "T", Base = 1e12; }

template SIPrefix(long n) {
    static if(n < Kilo.Base)
        alias None SIPrefix;
    else static if(n < Mega.Base)
        alias Kilo SIPrefix;
    else static if(n < Giga.Base)
        alias Mega SIPrefix;
    else static if(n < Tera.Base)
        alias Giga SIPrefix;
    else
        alias Tera SIPrefix;
}

template SIFormat(long n) {
    enum SIFormat = ToString!(n / SIPrefix!(n).Base) ~ SIPrefix!(n).Symbol;
}

private template ToString(real x) {
    enum ToString = ToString!(cast(long)x) ~ "." ~ ToString!(cast(long)(x * 10))[$ - 1];
}
private template ToString(long x) {
    enum ToString = std.metastrings.ToString!(x);
}


pragma(msg, SIFormat!(123));
pragma(msg, SIFormat!(234567));
pragma(msg, SIFormat!(345678901));
pragma(msg, SIFormat!(456789012345));
pragma(msg, SIFormat!(567890123456789));

static assert(false, "done!"); // コンパイル終了