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

# Currency (Using with)

## Example

```/*
Compile with the "TEST" version set, to output more information to the console during run.
*/

import std.string;
import std.math;

{
char[] buffer;
byte diff;

buffer = toString(num);
if (buffer.length < digits)
{
diff = digits - buffer.length;
for(int i=0; i < diff; i++)
buffer = "0" ~ buffer;
}
return buffer;
}

char[] toStringFromDouble(double d)
{
}

char[] toStringFromDouble(double d, int decPlaces)
{
}

char[] toStringFromDouble(double i, int decPlaces, char[] sep)
{
int whole;
int dec;
double decPart;

whole = cast(int) i;
version(TEST) printf("whole: %d\n", whole);

decPart = (i - whole) * pow(cast(real) 10, cast(uint) decPlaces);
version(TEST) printf("decPart: %lf\n", decPart);

dec = cast(int) (decPart);
version(TEST) printf("dec: %d\n", dec);
}

class Currency
{
double value() {return intValue;}

/* Write property ("set-ter") */
double value(double v) {return intValue = v;}

this(double v)
{
version(TEST) printf("An instance of the Currency object is created.\n\n");
intValue = v;
intSeparator = ".";
intDecPlaces = 2;
}

this()
{
this(0);
}

{
Currency t;
t.value = m.value + n.value;
return t;
}

{
intValue += m.value;
return this;
}

private
{
double intValue;
double intDecPlaces;
char[] intSeparator; /* decimal, period (U.S.), comma (Some non-U.S. locations) */
}
}

void main()
{
Currency c; // = new Currency();

with(c = new Currency())
{
printf("\n\n\t\t\t\t\t\t%.*s\n", toString);

printf("Change the value\t\t\t\t");
value = 100.75;
printf("%.*s\n", toString);
}

Currency c2;
with(c2 = new Currency(26.12))
{
printf("\t\t\t\t\t\t%.*s\n", toString);

printf("Change the value\t\t\t\t");
value = 6.25;
printf("%.*s\n", toString);