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

Changes between Version 1 and Version 2 of CurrencyExample

Show
Ignore:
Author:
jcc7 (IP: 192.149.244.9)
Timestamp:
11/06/06 18:23:49 (15 years ago)
Comment:

updated to use writefln

Legend:

Unmodified
Added
Removed
Modified
  • CurrencyExample

    v1 v2  
    1111{{{ 
    1212#!d 
    13 /* 
    14     Compile with the "TEST" version set to output more information to the console during run. 
    15 */ 
    16  
    17  
     13import std.stdio; 
    1814import std.string; 
    1915import std.math; 
    3632 
    3733 
    38 char[] toStringFromDouble(double d) 
    39 { 
    40     return toStringFromDouble(d, 2); /* defaults to 2 decimal places */  
    41 } 
    42  
    43 char[] toStringFromDouble(double d, int decPlaces) 
    44 { 
    45     return toStringFromDouble(d, decPlaces, "."); /* separator defaults to period */  
    46  
    47 } 
    48  
    49 char[] toStringFromDouble(double i, int decPlaces, char[] sep) 
    50  
    51     int whole; 
    52     int dec; 
    53     double decPart; 
    54      
    55     whole = cast(int) i; 
    56     version(TEST) printf("whole: %d\n", whole); 
    57  
    58     decPart = (i - whole) * pow(cast(real) 10, cast(uint) decPlaces); 
    59     version(TEST) printf("decPart: %lf\n", decPart); 
    60      
    61     dec = cast(int) (decPart); 
    62     version(TEST) printf("dec: %d\n", dec); 
    63     return toString(i) ~ sep ~ leadingZeros(dec, decPlaces); 
    64 } 
    65  
    66  
    6734class Currency 
    6835{    
    7643    this(double v) 
    7744    { 
    78         version(TEST) printf("An instance of the Currency object is created.\n\n"); 
     45        version(TEST) writefln("An instance of the Currency object is created.\n"); 
    7946        intValue = v; 
    80         intSeparator = "."; 
    81         intDecPlaces = 2; 
    8247    } 
    8348     
    8853    } 
    8954 
    90     char[] toString() { return toStringFromDouble(intValue, intDecPlaces, intSeparator);} 
     55    char[] toString() { return std.string.toString(intValue); }  
    9156 
    9257 
    11075    { 
    11176        double intValue; 
    112         double intDecPlaces; 
    113         char[] intSeparator; /* decimal, period (U.S.), comma (Some non-U.S. locations) */ 
    114  
    11577    } 
    11678} 
    12082{  
    12183    Currency c = new Currency();   
    122     printf("\n\t\t\t\t\t\t%.*s\n", c.toString); 
     84    writefln("\n\t\t\t\t\t\t%s", c.toString); 
    12385 
    124     printf("Change the value:\t\t\t\t"); 
     86    writef("Change the value:\t\t\t\t"); 
    12587    c.value = 100.75; 
    126     printf("%.*s\n", c.toString); 
     88    writefln("%s", c.toString); 
    12789 
    12890    Currency c2 = new Currency(26.12); 
    129     printf("\t\t\t\t\t\t%.*s\n", c2.toString); 
     91    writefln("\t\t\t\t\t\t%s", c2.toString); 
    13092 
    131     printf("Change the value:\t\t\t\t"); 
     93    writef("Change the value:\t\t\t\t"); 
    13294    c2.value = 6.25; 
    133     printf("%.*s\n", c2.toString); 
     95    writefln("%s", c2.toString); 
    13496 
    135     printf("Change the value (operator overloading):\t"); 
     97    writef("Change the value (operator overloading):\t"); 
    13698    c2 += c; /* requires operator overloading */ 
    13799 
    138     printf("%.*s\n\n", c2.toString); 
     100    writefln("%s\n", c2.toString); 
    139101} 
    140102}}} 
    141103 
    142 == Source == 
    143104 
    144 || Link || http://www.dsource.org/tutorials/index.php?show_example=43 || 
    145 || Posted by || jcc7 || 
    146 || Date/Time || Wed May 19, 2004 5:51 pm || 
     105== Example batch file == 
     106 
     107{{{ 
     108@echo off 
     109set pgm=CurrencyExample 
     110dmd %pgm%.d 
     111%pgm%.exe 
     112pause 
     113erase %pgm%.obj 
     114erase %pgm%.map 
     115}}} 
     116 
     117 
     118 
     119== Output == 
     120 
     121{{{ 
     122An instance of the Currency object is created. 
     123 
     124 
     125                                                0 
     126Change the value:                               100.75 
     127An instance of the Currency object is created. 
     128 
     129                                                26.12 
     130Change the value:                               6.25 
     131Change the value (operator overloading):        107 
     132}}} 
     133 
     134== Tests == 
     135 * Tested with DMD 0.173. 
     136 * Comiles and runs on Windows 2000.