From a01d5fa134aeafba4ba4e6a5f1bc72c7a92f3686 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Mon, 15 Jun 2015 00:27:24 +0200 Subject: [PATCH] Fix for floating precision rounding error --- .../v5/Financial/CurrencyTextBox.ascx.cs | 25 ++++++++++++++++++- .../v5/Financial/CreateInboundInvoice.aspx.cs | 6 ++--- .../v5/Financial/FileExpenseClaim.aspx.cs | 4 +-- .../v5/Financial/RequestCashAdvance.aspx.cs | 5 ++-- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/Site5/Controls/v5/Financial/CurrencyTextBox.ascx.cs b/Site5/Controls/v5/Financial/CurrencyTextBox.ascx.cs index 907d65ee..7e029da4 100644 --- a/Site5/Controls/v5/Financial/CurrencyTextBox.ascx.cs +++ b/Site5/Controls/v5/Financial/CurrencyTextBox.ascx.cs @@ -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 diff --git a/Site5/Pages/v5/Financial/CreateInboundInvoice.aspx.cs b/Site5/Pages/v5/Financial/CreateInboundInvoice.aspx.cs index dfb48898..71ae296f 100644 --- a/Site5/Pages/v5/Financial/CreateInboundInvoice.aspx.cs +++ b/Site5/Pages/v5/Financial/CreateInboundInvoice.aspx.cs @@ -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 diff --git a/Site5/Pages/v5/Financial/FileExpenseClaim.aspx.cs b/Site5/Pages/v5/Financial/FileExpenseClaim.aspx.cs index 134709c7..c36fc076 100644 --- a/Site5/Pages/v5/Financial/FileExpenseClaim.aspx.cs +++ b/Site5/Pages/v5/Financial/FileExpenseClaim.aspx.cs @@ -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; diff --git a/Site5/Pages/v5/Financial/RequestCashAdvance.aspx.cs b/Site5/Pages/v5/Financial/RequestCashAdvance.aspx.cs index 074b7a1d..f1a9041b 100644 --- a/Site5/Pages/v5/Financial/RequestCashAdvance.aspx.cs +++ b/Site5/Pages/v5/Financial/RequestCashAdvance.aspx.cs @@ -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;