Fix for floating precision rounding error

This commit is contained in:
Rick Falkvinge
2015-06-15 00:27:24 +02:00
parent d58feec70d
commit a01d5fa134
4 changed files with 31 additions and 9 deletions
@@ -14,7 +14,31 @@ namespace Swarmops.Frontend.Controls.v5.Financial
{
}
public Int64 Cents
{
get
{
return (Int64) (InternalValue * 100.0 + 0.5); // the +0.5 is to compensate for floating point errors. And yes, THEY HAPPEN.
}
set { this.Input.Text = (value / 100.0).ToString("N2", CultureInfo.CurrentCulture); }
}
public Int64 Metacents // Metacents are cents of cents, so four decimals to the currency unit.
{
get
{
return (Int64)(InternalValue * 10000.0 + 0.5); // the +0.5 is to compensate for floating point errors. And yes, THEY HAPPEN.
}
set { this.Input.Text = (value / 10000.0).ToString("N4", CultureInfo.CurrentCulture); }
}
[Obsolete ("Use Cents or Metacents", true)]
public double Value
{
get { throw new NotImplementedException(); }
}
internal double InternalValue
{
get
{
@@ -37,7 +61,6 @@ namespace Swarmops.Frontend.Controls.v5.Financial
throw new InvalidCastException("The text field cannot be parsed to a Double: \"" + contents + "\"");
}
set { this.Input.Text = value.ToString ("N2", CultureInfo.CurrentCulture); }
}
// TODO: Add other-currency parsing
@@ -99,7 +99,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial
{
// Prime bank details
this.CurrencyAmount.Value = 0;
this.CurrencyAmount.Cents = 0;
this.TextSupplier.Focus();
this.TextDueDate.Text = DateTime.Today.AddDays (30).ToShortDateString(); // Use current culture
@@ -136,7 +136,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial
// The data has been validated client-side already. We'll throw unfriendly exceptions if invalid data is passed here.
// People who choose to disable JavaScript and then submit bad input almost deserve to be hurt.
Int64 amountCents = (Int64) (this.CurrencyAmount.Value*100.0);
Int64 amountCents = this.CurrencyAmount.Cents;
string description = this.TextPurpose.Text;
@@ -197,7 +197,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial
this.TextAccount.Text = String.Empty;
this.TextPurpose.Text = String.Empty;
this.TextReference.Text = String.Empty;
this.CurrencyAmount.Value = 0;
this.CurrencyAmount.Cents = 0;
this.TextDueDate.Text = DateTime.Today.AddDays (30).ToShortDateString(); // Use current culture
// the easyUI combo fields should reset automatically on form submission unless we explicitly reconstruct
@@ -108,7 +108,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial
this.TextBank.Text = CurrentUser.BankName;
this.TextClearing.Text = CurrentUser.BankClearing;
this.TextAccount.Text = CurrentUser.BankAccount;
this.CurrencyAmount.Value = 0;
this.CurrencyAmount.Cents = 0;
this.CurrencyAmount.Focus();
Localize();
@@ -145,7 +145,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial
// The data has been validated client-side already. We'll throw unfriendly exceptions if invalid data is passed here.
// People who choose to disable JavaScript and then submit bad input almost deserve to be hurt.
Int64 amountCents = (Int64) (this.CurrencyAmount.Value*100.0);
Int64 amountCents = this.CurrencyAmount.Cents;
string description = this.TextPurpose.Text;
@@ -30,7 +30,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial
this.TextBank.Text = CurrentUser.BankName;
this.TextClearing.Text = CurrentUser.BankClearing;
this.TextAccount.Text = CurrentUser.BankAccount;
this.TextAmount.Value = 0.0;
this.TextAmount.Cents = 0;
this.TextAmount.Focus();
Localize();
@@ -63,8 +63,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial
{
// The data has been validated client-side already. We'll throw unfriendly exceptions if invalid data is passed here.
double amount = this.TextAmount.Value;
Int64 amountCents = (Int64) (amount*100.0);
Int64 amountCents = this.TextAmount.Cents;
string description = this.TextPurpose.Text;