Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changes between Version 4 and Version 5 of FormatNumberExample

Show
Ignore:
Author:
Andrej08 (IP: 78.2.39.16)
Timestamp:
09/07/10 02:55:18 (11 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • FormatNumberExample

    v4 v5  
    1010 
    1111== Example == 
    12 === D1.x === 
    1312{{{ 
    1413#!d 
    3433} 
    3534 
    36  
    37 version(example) import std.stdio; /* for writefln (for un) */ 
    38  
    39 unittest 
    40 { 
    41     version(example) writefln("Running unittest..."); 
    42     assert(formatNumber("100")=="100"); 
    43     assert(formatNumber("1000")=="1,000"); 
    44     assert(formatNumber("10000000")=="10,000,000"); 
    45     version(example) writefln("unittest Passed!"); 
    46 } 
    47  
    48  
    49 version(example) void main() /* Usage Example */ 
    50 { 
    51     writefln(formatNumber(1));  
    52     writefln(formatNumber(12)); 
    53     writefln(formatNumber(123));  
    54     writefln(formatNumber(1234));  
    55     writefln(formatNumber(12345));  
    56     writefln(formatNumber(123456));  
    57     writefln(formatNumber(1234567));  
    58     writefln(formatNumber(12345678));  
    59     writefln(formatNumber(123456789));  
    60     writefln(formatNumber(1234567890));  
    61 } 
    62 }}} 
    63 === D2.x === 
    64 {{{ 
    65 #!d 
    66 import std.conv : to; 
    67 import std.contracts : assumeUnique; 
    68  
    69  
    70 string formatNumber(int i)  { return formatNumber(to!string(i)); } 
    71 string formatNumber(long i) { return formatNumber(to!string(i)); } 
    72  
    73 string formatNumber(const(char)[] n) 
    74 { 
    75     char[] number=n.dup; 
    76     for(int i=n.length-3; i>=1; i-=3) 
    77     { 
    78         number=number[0..i]~","~number[i..$]; 
    79     } 
    80     return assumeUnique(number); 
    81 } 
    82  
    83  
    84 version(example) import std.stdio; /* for writefln (for un) */ 
    85  
    86 unittest 
    87 { 
    88     version(example) writefln("Running unittest..."); 
    89     assert(formatNumber("100")=="100"); 
    90     assert(formatNumber("1000")=="1,000"); 
    91     assert(formatNumber("10000000")=="10,000,000"); 
    92     version(example) writefln("unittest Passed!"); 
    93 } 
    94  
    95 version(example) void main() /* Usage Example */ 
    96 { 
    97     writefln(formatNumber(1));  
    98     writefln(formatNumber(12)); 
    99     writefln(formatNumber(123));  
    100     writefln(formatNumber(1234));  
    101     writefln(formatNumber(12345));  
    102     writefln(formatNumber(123456));  
    103     writefln(formatNumber(1234567));  
    104     writefln(formatNumber(12345678));  
    105     writefln(formatNumber(123456789));  
    106     writefln(formatNumber(1234567890));  
    107 } 
    108 }}} 
    109  
    110 === D2.x Templated === 
    111 {{{ 
    112 #!d 
    113 import std.conv : to; 
    114 import std.contracts : assumeUnique; 
    115  
    116  
    117 string formatNumber(Number)(Number n) 
    118 { 
    119     char[] number=to!(char[])(n); 
    120     for(int i=number.length-3; i>=1; i-=3) 
    121     { 
    122         number=number[0..i]~","~number[i..$]; 
    123     } 
    124     return assumeUnique(number); 
    125 } 
    12635 
    12736version(example) import std.stdio; /* for writefln (for un) */