Fractional Digits Preparation Support

This commit is contained in:
Olof Larsson 2014-10-22 07:49:51 +02:00
parent 7f48580e51
commit b083182291
4 changed files with 44 additions and 2 deletions

View File

@ -73,12 +73,22 @@ public class Money
return mixin.plural();
}
// -------------------------------------------- //
// FRACTIONAL DIGITS
// -------------------------------------------- //
public static int fractionalDigits()
{
if (disabled()) return 0;
return mixin.fractionalDigits();
}
public static double prepare(double amount)
{
if (disabled()) return amount;
return mixin.prepare(amount);
}
// -------------------------------------------- //
// EXISTANCE
// -------------------------------------------- //

View File

@ -18,7 +18,13 @@ public interface MoneyMixin
public String format(double amount, boolean includeUnit);
public String singular();
public String plural();
// -------------------------------------------- //
// FRACTIONAL DIGITS
// -------------------------------------------- //
public int fractionalDigits();
public double prepare(double amount);
// -------------------------------------------- //
// EXISTANCE

View File

@ -15,6 +15,30 @@ public abstract class MoneyMixinAbstract implements MoneyMixin
return this.format(amount, true);
}
// -------------------------------------------- //
// FRACTIONAL DIGITS
// -------------------------------------------- //
@Override
public double prepare(double amount)
{
final int fractionalDigits = this.fractionalDigits();
// -1 means infinite amount of fractional digits
if (fractionalDigits < 0) return amount;
// 0 means no fractional digits
if (fractionalDigits == 0) return Math.ceil(amount);
// OK! I'll have to calculate :P
int factor = (int) Math.round(Math.pow(10, fractionalDigits));
amount = amount * factor;
amount = Math.ceil(amount);
amount = amount / factor;
return amount;
}
// -------------------------------------------- //
// MOVE
// -------------------------------------------- //

View File

@ -100,6 +100,10 @@ public class MoneyMixinVault extends MoneyMixinAbstract
return this.economy.currencyNamePlural();
}
// -------------------------------------------- //
// FRACTIONAL DIGITS
// -------------------------------------------- //
@Override
public int fractionalDigits()
{
@ -204,6 +208,4 @@ public class MoneyMixinVault extends MoneyMixinAbstract
return true;
}
}