Changes between Version 1 and Version 2 of CurrencyExample
- Timestamp:
- 11/06/06 18:23:49 (17 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
CurrencyExample
v1 v2 11 11 {{{ 12 12 #!d 13 /* 14 Compile with the "TEST" version set to output more information to the console during run. 15 */ 16 17 13 import std.stdio; 18 14 import std.string; 19 15 import std.math; 36 32 37 33 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 67 34 class Currency 68 35 { 76 43 this(double v) 77 44 { 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"); 79 46 intValue = v; 80 intSeparator = ".";81 intDecPlaces = 2;82 47 } 83 48 88 53 } 89 54 90 char[] toString() { return toStringFromDouble(intValue, intDecPlaces, intSeparator);}55 char[] toString() { return std.string.toString(intValue); } 91 56 92 57 110 75 { 111 76 double intValue; 112 double intDecPlaces;113 char[] intSeparator; /* decimal, period (U.S.), comma (Some non-U.S. locations) */114 115 77 } 116 78 } 120 82 { 121 83 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); 123 85 124 printf("Change the value:\t\t\t\t");86 writef("Change the value:\t\t\t\t"); 125 87 c.value = 100.75; 126 printf("%.*s\n", c.toString);88 writefln("%s", c.toString); 127 89 128 90 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); 130 92 131 printf("Change the value:\t\t\t\t");93 writef("Change the value:\t\t\t\t"); 132 94 c2.value = 6.25; 133 printf("%.*s\n", c2.toString);95 writefln("%s", c2.toString); 134 96 135 printf("Change the value (operator overloading):\t");97 writef("Change the value (operator overloading):\t"); 136 98 c2 += c; /* requires operator overloading */ 137 99 138 printf("%.*s\n\n", c2.toString);100 writefln("%s\n", c2.toString); 139 101 } 140 102 }}} 141 103 142 == Source ==143 104 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 109 set pgm=CurrencyExample 110 dmd %pgm%.d 111 %pgm%.exe 112 pause 113 erase %pgm%.obj 114 erase %pgm%.map 115 }}} 116 117 118 119 == Output == 120 121 {{{ 122 An instance of the Currency object is created. 123 124 125 0 126 Change the value: 100.75 127 An instance of the Currency object is created. 128 129 26.12 130 Change the value: 6.25 131 Change the value (operator overloading): 107 132 }}} 133 134 == Tests == 135 * Tested with DMD 0.173. 136 * Comiles and runs on Windows 2000.