Skip to content
This repository has been archived by the owner on Jun 6, 2020. It is now read-only.

Commit

Permalink
Apply dfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Zwinkau committed May 28, 2017
1 parent e805083 commit aee83be
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions source/money.d
Expand Up @@ -68,6 +68,7 @@ import std.traits : hasMember;
pure @safe nothrow private string decimals_format(int x)()
{
import std.conv : text;

return "%0" ~ text(x) ~ "d";
}

Expand Down Expand Up @@ -257,14 +258,16 @@ struct currency(string currency_name, int dec_places = 4, roundingMode rmode = r
}

/// Can check equality with money amounts of the same concurrency and decimal places.
bool opEquals(OT)(auto ref const OT other) const if (isCurrency!OT
&& other.__currency == currency_name && other.__dec_places == dec_places)
bool opEquals(OT)(auto ref const OT other) const
if (isCurrency!OT && other.__currency == currency_name
&& other.__dec_places == dec_places)
{
return other.amount == amount;
}

/// Can compare with money amounts of the same concurrency.
int opCmp(OT)(const OT other) const if (isCurrency!OT && other.__currency == currency_name)
int opCmp(OT)(const OT other) const
if (isCurrency!OT && other.__currency == currency_name)
{
static if (dec_places == other.__dec_places)
{
Expand Down Expand Up @@ -306,7 +309,8 @@ struct currency(string currency_name, int dec_places = 4, roundingMode rmode = r
decimals = round!(rmode)(decimals, n);
decimals = decimals / pow10(n);
import std.conv : text;
formattedWrite(sink, "%0"~text(fmt.precision)~"d", decimals);

formattedWrite(sink, "%0" ~ text(fmt.precision) ~ "d", decimals);
}
else
{
Expand Down Expand Up @@ -382,26 +386,27 @@ unittest
static assert(!__traits(compiles, EURa(1) < USD(1)));
}


// TODO Using negative dec_places for big numbers?
//@nogc @safe unittest
//{
// alias USD = currency!("USD", -6);
// assert(USD(1_000_000.00) == USD(1_100_000.));
//}

enum isCurrency(T) = (hasMember!(T, "amount") && hasMember!(T, "__dec_places")
&& hasMember!(T, "__rmode"));
enum isCurrency(T) = (hasMember!(T, "amount") && hasMember!(T,
"__dec_places") && hasMember!(T, "__rmode"));
static assert(isCurrency!(currency!"EUR"));

// TODO @safe (due to std.format.format)
unittest
{
alias EUR = currency!("EUR");
import std.format : format;

assert(format("%s", EUR(3.1)) == "3.1000EUR");

import std.exception : assertThrown;

assertThrown!Exception(format("%x", EUR(3.1)));
}

Expand All @@ -415,6 +420,7 @@ unittest
assert(EUR(6) != EUR(5));

import std.exception : assertThrown;

assertThrown!OverflowException(EUR.max * 2);
assertThrown!OverflowException(EUR.max * 2.0);
}
Expand Down Expand Up @@ -695,6 +701,7 @@ real round(real x, roundingMode m) @trusted
assert(round(3.8, roundingMode.HALF_TO_ZERO) == 3.0);

import std.exception : assertThrown;

assertThrown!ForbiddenRounding(round(3.1, roundingMode.UNNECESSARY));
assertThrown!ForbiddenRounding(round(3.1, roundingMode.HALF_EVEN));
assertThrown!ForbiddenRounding(round(3.1, roundingMode.HALF_ODD));
Expand Down

0 comments on commit aee83be

Please sign in to comment.