PriceEntryControl - round with 'decimal' to avoid precision-loss

This commit is contained in:
John Colvin committed 2018-12-05 15:40:55 -06:00
1 parent c47bd12b54
commit db916ed7c7
2 files changed
+46 -7

No files matched your search

@@ -1,24 +1,44 @@
using bts.api;
using bts.utils;
using mbts.spark;
using mbts.ui.extensions;
using mbts.ui.spark.converters;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using static mbts.ui.utility.text.TextUtils;
namespace mbts.ui.spark {
/// <summary>
/// Interaction logic for PriceEntryControl.xaml
/// </summary>
class PriceEntryControl : Control {
double? _raw_value;
static PriceEntryControl() =>
DefaultStyleKeyProperty.OverrideMetadata(typeof(PriceEntryControl), new FrameworkPropertyMetadata(typeof(PriceEntryControl)));
#region Value
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(PriceEntryControl),new FrameworkPropertyMetadata(double.NaN, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, CoerceValue));
private static object CoerceValue(DependencyObject d, object baseValue) { return TickUtils.get_regional_ticks((ContractSpecificationImmutable)d.GetValue(ContractSpecProperty)).round((double)baseValue); }
public static readonly DependencyProperty ValueProperty =
WPFHelper.create_dp<double, PriceEntryControl>(
nameof(Value),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
null,
(o, v) => {
var spec = o.ContractSpec;
o._raw_value = v;
if (!spec.HasValue || DoubleHelper.IsNaNOrInfinity(v))
return v;
// Note: the decimal cast here is important bc double-rounding errors can cause the keyboard text to conflict with poorly rounded result, and negate Tick Up/Down commands.
// This happens bc the tick up/down commands flush the current text before inc/decrementing.
double tick = (double)spec.Value.round_to_tick((decimal)v);
if (MathUtils.epsilon_equals(tick, v)) {
// this value has already been rounded, don't make it re-enter the CoerceValueCallback delegate.
return v;
}
return tick;
},
double.NaN);
public double Value { get => (double)GetValue(ValueProperty); set => SetValue(ValueProperty, value); }
#endregion
@@ -40,9 +60,20 @@ namespace mbts.ui.spark {
#endregion
#region ContractSpec
public static readonly DependencyProperty ContractSpecProperty = DependencyProperty.Register("ContractSpec", typeof(ContractSpecificationImmutable), typeof(PriceEntryControl),
new PropertyMetadata(SecurityDataHelper.Defaults.ContractSpecificationImmutablePennies, OnContractSpecChanged));
static void OnContractSpecChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => ((PriceEntryControl)d)._update_text_binding();
public static readonly DependencyProperty ContractSpecProperty =
WPFHelper.create_dp<ContractSpecificationImmutable?, PriceEntryControl>(
nameof(ContractSpec),
(o, v) => {
if (o._raw_value.HasValue) {
// Refresh Value (rounding may have changed)
// Note: We don't call 'CoerceValue' bc the ValueProperty's CoerceValueCallback always overwrites '_raw_value.'
// If we called CoerceValue here, the callback would ultimately overwrite _raw_value with the previous ContractSpec's rounded value.
// This is a chicken/egg dilemma; we want to ensure Value is always round with respect to the ContractSpecification.
o.SetCurrentValue(ValueProperty, o._raw_value.Value);
}
o._update_text_binding();
}, SecurityDataHelper.Defaults.ContractSpecificationImmutable);
public ContractSpecificationImmutable? ContractSpec { get => GetValue(ContractSpecProperty) as ContractSpecificationImmutable?; set => SetValue(ContractSpecProperty, value); }
#endregion
@@ -123,6 +123,14 @@ namespace mbts.spark {
return intpart + floatpart;
}
public static decimal round_to_tick(this ContractSpecificationImmutable spec, decimal value) {
decimal intpart = (long)value;
decimal floatpart = value % 1;
decimal tick = (decimal)spec.ticksize((double)value);
floatpart = ((int)(floatpart / tick)) * tick;
return intpart + floatpart;
}
public static RegionalTickFactors get_regional_ticks(ContractSpecificationImmutable spec) =>
TickStore.get(spec);