refactor: Clean up TickExtensions
This commit is contained in:
1 parent
4b27bbe490
commit
7845b3fdc1
1 file changed
+208
-152
@@ -1,17 +1,17 @@
|
||||
using bts;
|
||||
using bts.api;
|
||||
using bts.utils;
|
||||
using mbts.spark;
|
||||
using mbts.user.settings;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using static bts.utils.MathUtils;
|
||||
|
||||
namespace mbts {
|
||||
using bts;
|
||||
using bts.api;
|
||||
using bts.utils;
|
||||
|
||||
using mbts.spark;
|
||||
using mbts.user.settings;
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
public static class TickExtensions {
|
||||
public static string _nan_text => TickDisplaySettings.Default.nan_text;
|
||||
static string _nan_text => TickDisplaySettings.Default.nan_text;
|
||||
|
||||
#region Public Extensions (is_valid, format, parse, tick_up, tick_down)
|
||||
public static bool is_valid(this TickFactors tick) =>
|
||||
(tick.tick_unit > 0 && tick.tick_unit <= (tick.tick_base * 0.5)) ||
|
||||
tick.tick_unit == tick.tick_base;
|
||||
@@ -25,7 +25,7 @@ namespace mbts {
|
||||
public static double tick_unit_frac(this TickFactors tick) =>
|
||||
tick.tick_unit / tick.tick_base;
|
||||
|
||||
public static double reduced_base(this TickFactors tick) => factor_out(tick.tick_base, 10);
|
||||
public static double reduced_base(this TickFactors tick) => MathUtils.factor_out(tick.tick_base, 10);
|
||||
|
||||
public static string format_without_decimal_part(this TickFactors tick, double value) =>
|
||||
format(tick, value, double.NaN, false, _nan_text, true);
|
||||
@@ -37,12 +37,18 @@ namespace mbts {
|
||||
format(tick, value, double.NaN, show_commas, _nan_text);
|
||||
|
||||
public static string format(this TickFactors tick, double value, TickDisplaySettings settings) {
|
||||
if (settings is null)
|
||||
if (settings is null) {
|
||||
settings = TickDisplaySettings.Default;
|
||||
if (double.IsNaN(value)) return settings.nan_text;
|
||||
if (double.IsInfinity(value)) return value.ToString();
|
||||
if (!tick.is_valid()) return _format_no_tick(value, settings.display_multiplier, settings.show_commas);
|
||||
return _format_tick(tick, value, settings);
|
||||
}
|
||||
if (double.IsNaN(value)) {
|
||||
return settings.nan_text;
|
||||
}
|
||||
if (double.IsInfinity(value)) {
|
||||
return value.ToString();
|
||||
}
|
||||
return tick.is_valid() ?
|
||||
_format_tick(tick, value, settings) :
|
||||
_format_no_tick(value, settings.display_multiplier, settings.show_commas);
|
||||
}
|
||||
|
||||
public static string format(
|
||||
@@ -52,39 +58,54 @@ namespace mbts {
|
||||
bool show_commas,
|
||||
string nan_text,
|
||||
bool show_decimal_part = true) =>
|
||||
tick.format(value, TickDisplaySettings.Default.with(
|
||||
display_multiplier: display_multiplier,
|
||||
show_commas: show_commas,
|
||||
nan_text: nan_text,
|
||||
show_decimal_part: show_decimal_part));
|
||||
tick.format(
|
||||
value,
|
||||
TickDisplaySettings.Default.with(
|
||||
display_multiplier: display_multiplier,
|
||||
show_commas: show_commas,
|
||||
nan_text: nan_text,
|
||||
show_decimal_part: show_decimal_part));
|
||||
|
||||
public static string format_without_decimal_part(this RegionalTickFactors regional_tick, double value) =>
|
||||
format_without_decimal_part(value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick, value);
|
||||
format_without_decimal_part(
|
||||
value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick,
|
||||
value);
|
||||
|
||||
public static string format(this RegionalTickFactors regional_tick, double value, TickDisplaySettings settings = null) =>
|
||||
format(value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick, value, settings);
|
||||
format(
|
||||
value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick,
|
||||
value,
|
||||
settings);
|
||||
|
||||
public static string format(this RegionalTickFactors regional_tick, double value, double display_multiplier, bool show_commas, string nan_text) =>
|
||||
format(value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick, value, display_multiplier, show_commas, nan_text);
|
||||
format(
|
||||
value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick,
|
||||
value,
|
||||
display_multiplier,
|
||||
show_commas, nan_text);
|
||||
|
||||
public static double parse(this TickFactors tick, string text, TickDisplaySettings display_settings) =>
|
||||
display_settings is null ?
|
||||
parse(tick, text, double.NaN, _nan_text) :
|
||||
parse(tick, text, display_settings.display_multiplier, display_settings.nan_text ?? string.Empty);
|
||||
|
||||
public static double parse(this TickFactors tick, string text, double display_multiplier, string nan_text) {
|
||||
if (!tick.is_valid()) return _parse_no_tick(text, display_multiplier);
|
||||
return _parse(tick, text, display_multiplier, nan_text);
|
||||
}
|
||||
public static double parse(this TickFactors tick, string text, double display_multiplier, string nan_text) =>
|
||||
tick.is_valid() ?
|
||||
_parse(tick, text, display_multiplier, nan_text) :
|
||||
_parse_no_tick(text, display_multiplier);
|
||||
|
||||
public static double parse(this RegionalTickFactors regional_tick, string text, double display_multiplier, string nan_text) {
|
||||
if (!regional_tick.is_valid()) return _parse_no_tick(text, display_multiplier);
|
||||
return _parse(regional_tick, text, display_multiplier, nan_text);
|
||||
}
|
||||
public static double parse(this RegionalTickFactors regional_tick, string text, double display_multiplier, string nan_text) =>
|
||||
regional_tick.is_valid() ?
|
||||
_parse(regional_tick, text, display_multiplier, nan_text) :
|
||||
_parse_no_tick(text, display_multiplier);
|
||||
|
||||
public static double tickup(this TickFactors tick, double input) =>
|
||||
tick.tickup(input, 1);
|
||||
|
||||
public static double tickup(this TickFactors tick, double input) { return tick.tickup(input, 1); }
|
||||
public static double tickup(this TickFactors tick, double input, uint count) {
|
||||
if (DoubleHelper.IsNaNOrInfinity(input)) return input;
|
||||
if (DoubleHelper.IsNaNOrInfinity(input)) {
|
||||
return input;
|
||||
}
|
||||
double tuf = tick.tick_unit_frac();
|
||||
input -= input % tuf;
|
||||
double val = input + tuf * count;
|
||||
@@ -97,11 +118,16 @@ namespace mbts {
|
||||
public static double tickup(this RegionalTickFactors regional_tick, double input, uint count) =>
|
||||
regional_tick.tickup(input, count, TickUtils.precision_const);
|
||||
|
||||
public static double tickup(this RegionalTickFactors regional_tick, double input, uint count, double precision, bool bling = true) {
|
||||
if (!precision.is_real_positive())
|
||||
throw new ArgumentException(nameof(precision), "Must be a real positive number.");
|
||||
if (DoubleHelper.IsNaNOrInfinity(input) || count == 0) return input;
|
||||
double val, tuf;
|
||||
public static double tickup(this RegionalTickFactors regional_tick, double input, uint count, double precision) {
|
||||
if (!precision.is_real_positive()) {
|
||||
throw new ArgumentException("Must be a real positive number.", nameof(precision));
|
||||
}
|
||||
if (DoubleHelper.IsNaNOrInfinity(input) || count == 0) {
|
||||
return input;
|
||||
}
|
||||
|
||||
double val;
|
||||
double tuf;
|
||||
if (input < regional_tick.low_tick_inclusive_end) {
|
||||
tuf = regional_tick.low_tick.tick_unit_frac();
|
||||
double ieeer = Math.IEEERemainder(input, tuf);
|
||||
@@ -123,76 +149,21 @@ namespace mbts {
|
||||
return _round_tick(tuf, val);
|
||||
}
|
||||
|
||||
public static ContractSpecification to_spec(this TickFactors tick) {
|
||||
if (tick == null)
|
||||
return null;
|
||||
double tick_frac = tick.tick_unit_frac();
|
||||
return new ContractSpecification {
|
||||
LowTick = tick_frac,
|
||||
HighTick = tick_frac,
|
||||
PriceBreak = double.MaxValue,
|
||||
MainFraction = (uint)factor_out(tick.tick_base, 10),
|
||||
PriceDisplayFormat = (uint)tick.display_digits(),
|
||||
PriceMultiplier = 1,
|
||||
CashPerPoint = 1
|
||||
};
|
||||
}
|
||||
public static double tickdown(this TickFactors tick, double input) =>
|
||||
tick.tickdown(input, 1);
|
||||
|
||||
public static bool try_get_tickdiff(this RegionalTickFactors regional_tick, double x, double y, out int n_low, out int n_high) {
|
||||
if (DoubleHelper.IsNaNOrInfinity(x) || DoubleHelper.IsNaNOrInfinity(y)) {
|
||||
n_low = default;
|
||||
n_high = default;
|
||||
return false;
|
||||
}
|
||||
(n_low, n_high) = tickdiff_unchecked(regional_tick, x, y);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Callers should be certain that <paramref name="x"/> and <paramref name="y"/> are not nan or infinity.
|
||||
/// </summary>
|
||||
/// <param name="regional_tick"></param>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="y"></param>
|
||||
internal static (int n_low, int n_high) tickdiff_unchecked(this RegionalTickFactors regional_tick, double x, double y) {
|
||||
double ltie = regional_tick.low_tick_inclusive_end;
|
||||
double raw_diff = x - y;
|
||||
if (x < ltie) {
|
||||
double low_unit_frac = regional_tick.low_tick.tick_unit_frac();
|
||||
if (y < ltie) {
|
||||
return (round_fractional_units(raw_diff, low_unit_frac), 0);
|
||||
} else {
|
||||
double high_unit_frac = regional_tick.high_tick.tick_unit_frac();
|
||||
double low_diff = ltie - x;
|
||||
double high_diff = raw_diff - low_diff;
|
||||
int n_low = round_fractional_units(low_diff, low_unit_frac);
|
||||
int n_high = round_fractional_units(high_diff, high_unit_frac);
|
||||
return (n_low, n_high);
|
||||
}
|
||||
} else {
|
||||
double high_unit_frac = regional_tick.high_tick.tick_unit_frac();
|
||||
if (y < ltie) {
|
||||
double low_unit_frac = regional_tick.low_tick.tick_unit_frac();
|
||||
double high_diff = x - ltie;
|
||||
double low_diff = raw_diff - high_diff;
|
||||
int n_low = round_fractional_units(low_diff, low_unit_frac);
|
||||
int n_high = round_fractional_units(high_diff, high_unit_frac);
|
||||
return (n_low, n_high);
|
||||
} else {
|
||||
return (0, round_fractional_units(raw_diff, high_unit_frac));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static double tickdown(this TickFactors tick, double input) { return tick.tickdown(input, 1); }
|
||||
public static double tickdown(this TickFactors tick, double input, uint count) {
|
||||
if (DoubleHelper.IsNaNOrInfinity(input)) return input;
|
||||
if (DoubleHelper.IsNaNOrInfinity(input)) {
|
||||
return input;
|
||||
}
|
||||
double tuf = tick.tick_unit_frac();
|
||||
double remainder = input % tuf;
|
||||
if (remainder > 0) input += tuf - remainder;
|
||||
if (remainder > 0) {
|
||||
input += tuf - remainder;
|
||||
}
|
||||
double val = input - tuf * count;
|
||||
return _round_tick(tuf, val);
|
||||
}
|
||||
}
|
||||
|
||||
public static double tickdown(this RegionalTickFactors regional_tick, double input) =>
|
||||
regional_tick.tickdown(input, 1);
|
||||
@@ -201,8 +172,9 @@ namespace mbts {
|
||||
tickdown(regional_tick, input, count, TickUtils.precision_const);
|
||||
|
||||
public static double tickdown(this RegionalTickFactors regional_tick, double input, uint count, double precision) {
|
||||
if (!precision.is_real_positive())
|
||||
throw new ArgumentException(nameof(precision), "Must be a real positive number.");
|
||||
if (!precision.is_real_positive()) {
|
||||
throw new ArgumentException("Must be a real positive number.", nameof(precision));
|
||||
}
|
||||
if (DoubleHelper.IsNaNOrInfinity(input) || count == 0) return input;
|
||||
double val, tuf;
|
||||
if (input > regional_tick.low_tick_inclusive_end) {
|
||||
@@ -226,18 +198,81 @@ namespace mbts {
|
||||
return _round_tick(tuf, val);
|
||||
}
|
||||
|
||||
public static double round(this TickFactors tick, double input) { return _round_tick(tick.tick_unit_frac(), input); }
|
||||
public static double round(this RegionalTickFactors regional_tick, double input) {
|
||||
if (DoubleHelper.IsNaNOrInfinity(input)) return input;
|
||||
return input <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick.round(input) : regional_tick.high_tick.round(input);
|
||||
public static ContractSpecification to_spec(this TickFactors tick) =>
|
||||
tick is null ? null :
|
||||
new ContractSpecification {
|
||||
LowTick = tick.tick_unit_frac(),
|
||||
HighTick = tick.tick_unit_frac(),
|
||||
PriceBreak = double.MaxValue,
|
||||
MainFraction = (uint)MathUtils.factor_out(tick.tick_base, 10),
|
||||
PriceDisplayFormat = (uint)tick.display_digits(),
|
||||
PriceMultiplier = 1,
|
||||
CashPerPoint = 1
|
||||
};
|
||||
|
||||
public static bool try_get_tickdiff(this RegionalTickFactors regional_tick, double minuend, double subtrahend, out int n_low, out int n_high) {
|
||||
if (DoubleHelper.IsNaNOrInfinity(minuend) || DoubleHelper.IsNaNOrInfinity(subtrahend)) {
|
||||
n_low = default;
|
||||
n_high = default;
|
||||
return false;
|
||||
}
|
||||
(n_low, n_high) = tickdiff_unchecked(regional_tick, minuend, subtrahend);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes the difference, in tick units, between <paramref name="minuend"/> and <paramref name="subtrahend"/>
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Asserts that <paramref name="minuend"/> and <paramref name="subtrahend"/> are real numbers (i.e., not nan or infinity)
|
||||
/// </remarks>
|
||||
/// <param name="minuend">The minuend</param>
|
||||
/// <param name="subtrahend">The subtrahend</param>
|
||||
internal static (int n_low, int n_high) tickdiff_unchecked(this RegionalTickFactors regional_tick, double minuend, double subtrahend) {
|
||||
Log.assert(minuend.is_real() && subtrahend.is_real());
|
||||
|
||||
double ltie = regional_tick.low_tick_inclusive_end;
|
||||
double raw_diff = minuend - subtrahend;
|
||||
|
||||
if (minuend < ltie) {
|
||||
if (subtrahend < ltie) {
|
||||
return (MathUtils.round_fractional_units(raw_diff, regional_tick.low_tick.tick_unit_frac()), 0);
|
||||
}
|
||||
double low_diff = ltie - minuend;
|
||||
double high_diff = raw_diff - low_diff;
|
||||
int n_low = MathUtils.round_fractional_units(low_diff, regional_tick.low_tick.tick_unit_frac());
|
||||
int n_high = MathUtils.round_fractional_units(high_diff, regional_tick.high_tick.tick_unit_frac());
|
||||
return (n_low, n_high);
|
||||
}
|
||||
|
||||
if (subtrahend < ltie) {
|
||||
double high_diff = minuend - ltie;
|
||||
double low_diff = raw_diff - high_diff;
|
||||
int n_low = MathUtils.round_fractional_units(low_diff, regional_tick.high_tick.tick_unit_frac());
|
||||
int n_high = MathUtils.round_fractional_units(high_diff, regional_tick.high_tick.tick_unit_frac());
|
||||
return (n_low, n_high);
|
||||
}
|
||||
return (0, MathUtils.round_fractional_units(raw_diff, regional_tick.high_tick.tick_unit_frac()));
|
||||
}
|
||||
|
||||
public static double round(this TickFactors tick, double input) =>
|
||||
_round_tick(tick.tick_unit_frac(), input);
|
||||
|
||||
public static double round(this RegionalTickFactors regional_tick, double input) {
|
||||
if (DoubleHelper.IsNaNOrInfinity(input)) {
|
||||
return input;
|
||||
}
|
||||
return input <= regional_tick.low_tick_inclusive_end ?
|
||||
regional_tick.low_tick.round(input) :
|
||||
regional_tick.high_tick.round(input);
|
||||
}
|
||||
|
||||
public static int display_digits(this TickFactors tick) {
|
||||
if (tick.tick_unit == tick.tick_base) {
|
||||
return (int)Math.Floor(Math.Log10(tick.tick_base));
|
||||
}
|
||||
return display_digits(tick.tick_base);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public static int display_digits(double tick_base) {
|
||||
double log_tick_base = Math.Log10(tick_base);
|
||||
@@ -249,34 +284,44 @@ namespace mbts {
|
||||
public static TickFactors with(this TickFactors tick_factors, double? tick_base = null, double? tick_unit = null) =>
|
||||
new(tick_base ?? tick_factors.tick_base, tick_unit ?? tick_factors.tick_unit);
|
||||
|
||||
#region Private Members
|
||||
private static char separator(this TickFactors tick) {
|
||||
static char _separator(TickFactors tick) {
|
||||
double tb_log10 = Math.Log10(tick.tick_base);
|
||||
return tb_log10 >= 1 && tb_log10 % 1 == 0 ? '.' : '-';
|
||||
}
|
||||
|
||||
private static string _format_no_tick(double value, double display_multiplier, bool show_commas) {
|
||||
if (!double.IsNaN(display_multiplier)) value *= display_multiplier;
|
||||
static string _format_no_tick(double value, double display_multiplier, bool show_commas) {
|
||||
if (!double.IsNaN(display_multiplier)) {
|
||||
value *= display_multiplier;
|
||||
}
|
||||
return value.ToString(show_commas ? "#,0" : "0");
|
||||
}
|
||||
|
||||
private static double _parse_no_tick(string text, double display_multiplier) {
|
||||
double result;
|
||||
if (!double.TryParse(text, out result)) return double.NaN;
|
||||
static double _parse_no_tick(string text, double display_multiplier) {
|
||||
if (!double.TryParse(text, out var result)) {
|
||||
return double.NaN;
|
||||
}
|
||||
return double.IsNaN(display_multiplier) || display_multiplier == 0.0 ? result : result / display_multiplier;
|
||||
}
|
||||
|
||||
private static double _get_integer_part(string integer_text) {
|
||||
return string.IsNullOrEmpty(integer_text) ? 0 : int.TryParse(integer_text, NumberStyles.Any, CultureInfo.CurrentCulture, out int result) ? result : double.NaN;
|
||||
static double _get_integer_part(string integer_text) {
|
||||
if (integer_text.is_null_or_white_space()) {
|
||||
return 0;
|
||||
}
|
||||
return int.TryParse(integer_text, NumberStyles.Any, CultureInfo.CurrentCulture, out var result) ? result : double.NaN;
|
||||
}
|
||||
|
||||
private static double _get_numerator_part(string numerator_text, double tick_base, double tick_unit) {
|
||||
if (string.IsNullOrEmpty(numerator_text)) return 0;
|
||||
double numerator_part;
|
||||
if (!double.TryParse(numerator_text, out numerator_part)) return double.NaN;
|
||||
static double _get_numerator_part(string numerator_text, double tick_base) {
|
||||
if (string.IsNullOrEmpty(numerator_text)) {
|
||||
return 0;
|
||||
}
|
||||
if (!double.TryParse(numerator_text, out var numerator_part)) {
|
||||
return double.NaN;
|
||||
}
|
||||
|
||||
//e.g [tick_base 64] 1989-80 == 1989-8, or 1989-320 == 1989-32
|
||||
if (numerator_part >= tick_base) numerator_part /= Math.Pow(10, Math.Ceiling(Math.Log10(numerator_part) - Math.Log10(tick_base)));
|
||||
else {
|
||||
if (numerator_part >= tick_base) {
|
||||
numerator_part /= Math.Pow(10, Math.Ceiling(Math.Log10(numerator_part) - Math.Log10(tick_base)));
|
||||
} else {
|
||||
//e.g [tick_base 640] 1989-8 == 1989-80, or 1989-32 == 1989-320
|
||||
int ndd_diff = display_digits(tick_base) - numerator_text.Length;
|
||||
if (ndd_diff > 0) numerator_part *= Math.Pow(10, ndd_diff);
|
||||
@@ -285,49 +330,62 @@ namespace mbts {
|
||||
return numerator_part;
|
||||
}
|
||||
|
||||
private static double _parse(RegionalTickFactors regional_tick, string text, double display_multiplier, string nan_text) {
|
||||
static double _parse(RegionalTickFactors regional_tick, string text, double display_multiplier, string nan_text) {
|
||||
double result = _parse(regional_tick.low_tick, text, display_multiplier, nan_text);
|
||||
double ltie = double.IsNaN(display_multiplier) ? regional_tick.low_tick_inclusive_end : regional_tick.low_tick_inclusive_end * display_multiplier;
|
||||
if (result > ltie) result = _parse(regional_tick.high_tick, text, display_multiplier, nan_text);
|
||||
double ltie = display_multiplier.is_nan() ?
|
||||
regional_tick.low_tick_inclusive_end :
|
||||
regional_tick.low_tick_inclusive_end * display_multiplier;
|
||||
if (result > ltie) {
|
||||
result = _parse(regional_tick.high_tick, text, display_multiplier, nan_text);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static double _parse(TickFactors tick, string text, double display_multiplier, string nan_text) {
|
||||
if (string.IsNullOrWhiteSpace(text) || text == nan_text) return double.NaN;
|
||||
static double _parse(TickFactors tick, string text, double display_multiplier, string nan_text) {
|
||||
if (text.is_null_or_white_space() || text == nan_text) {
|
||||
return double.NaN;
|
||||
}
|
||||
text = text.Replace(" ", string.Empty);
|
||||
if (text[0] == '-') return text.Length > 1 ? -_parse(tick, text.Substring(1), display_multiplier, nan_text) : double.NaN;
|
||||
double d;
|
||||
if (double.TryParse(text, out d)) return double.IsNaN(display_multiplier) ? d : d * display_multiplier;
|
||||
var parts = text.Split(tick.separator());
|
||||
if (text[0] == '-') {
|
||||
return text.Length > 1 ? -_parse(tick, text[1..], display_multiplier, nan_text) : double.NaN;
|
||||
}
|
||||
if (double.TryParse(text, out var d)) {
|
||||
return double.IsNaN(display_multiplier) ? d : d * display_multiplier;
|
||||
}
|
||||
var parts = text.Split(_separator(tick));
|
||||
double integer_part = _get_integer_part(parts[0]);
|
||||
double result = parts.Length == 1 ? integer_part : _parse(tick, integer_part, parts[1]);
|
||||
return double.IsNaN(display_multiplier) ? result : display_multiplier * result;
|
||||
}
|
||||
|
||||
private static double _parse(TickFactors tick, double integer_part, string numerator_part_text) {
|
||||
double numerator_part = _get_numerator_part(numerator_part_text, tick.tick_base, tick.tick_unit);
|
||||
static double _parse(TickFactors tick, double integer_part, string numerator_part_text) {
|
||||
double numerator_part = _get_numerator_part(numerator_part_text, tick.tick_base);
|
||||
return integer_part + numerator_part / tick.tick_base;
|
||||
}
|
||||
|
||||
private static string _format_tick(TickFactors tick, double value, TickDisplaySettings settings) {
|
||||
if (!settings.display_multiplier.is_nan())
|
||||
static string _format_tick(TickFactors tick, double value, TickDisplaySettings settings) {
|
||||
if (!settings.display_multiplier.is_nan()) {
|
||||
value *= settings.display_multiplier;
|
||||
}
|
||||
// Hide the sign from the 'core_impl.' If the value is negative,
|
||||
// flip it before calling core_impl, then prepend a '-' to the resultant string.
|
||||
bool negate = value < 0;
|
||||
if (negate)
|
||||
if (negate) {
|
||||
value = -value;
|
||||
}
|
||||
var text = core_impl();
|
||||
if (!settings.show_zero_integer) {
|
||||
// Hide the integer if its zero. This could be better optimized,
|
||||
// but speed shouldn't be an issue, so it doesn't seem worth the time/effort.
|
||||
var parts = text.Split(tick.separator());
|
||||
var parts = text.Split(_separator(tick));
|
||||
if (parts.Length > 1 && parts[0] == "0")
|
||||
text = parts[1];
|
||||
}
|
||||
if (negate)
|
||||
if (negate) {
|
||||
text = "-" + text;
|
||||
}
|
||||
return text;
|
||||
|
||||
// Formats a 'normalized' value. This method expects 'value' to be positive,
|
||||
// and [already] multiplied by the display multiplier (if non-nan).
|
||||
string core_impl() {
|
||||
@@ -339,12 +397,13 @@ namespace mbts {
|
||||
int n_units = (int)Math.Round(numerator_dec / unit_frac);
|
||||
if (n_units == tick.tick_base) {
|
||||
if (settings.show_decimal_part) {
|
||||
return (integer_part + 1).ToString() + tick.separator() + new string('0',
|
||||
return (integer_part + 1).ToString() + _separator(tick) + new string('0',
|
||||
settings.pad_with_leading_zeros ? display_digits(tick.tick_base) : 1);
|
||||
} else {
|
||||
return integer_part.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
double decimal_part = n_units * tick.tick_unit;
|
||||
if (decimal_part == tick.tick_base) {
|
||||
++integer_part;
|
||||
@@ -354,25 +413,22 @@ namespace mbts {
|
||||
// for such cases, we need to truncate the decimal part rather than round it.
|
||||
decimal_part = Math.Floor(decimal_part);
|
||||
}
|
||||
|
||||
string integer_part_text = settings.show_commas ? integer_part.ToString("#,0") : integer_part.ToString();
|
||||
if (settings.show_decimal_part) {
|
||||
return integer_part_text + tick.separator() + decimal_part.ToString(
|
||||
return integer_part_text + _separator(tick) + decimal_part.ToString(
|
||||
settings.pad_with_leading_zeros ? new string('0', display_digits(tick.tick_base)) : "0");
|
||||
} else {
|
||||
return integer_part_text;
|
||||
}
|
||||
return integer_part_text;
|
||||
}
|
||||
}
|
||||
catch (OverflowException ex) {
|
||||
} catch (OverflowException ex) {
|
||||
Log.Ui.warn($"Arithmetic overflow exception in format_tick (tick:{tick}, value:{value}):{ex}");
|
||||
return "Overflow";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static double _round_tick(double tick_unit_frac, double input) {
|
||||
return input - Math.IEEERemainder(input, tick_unit_frac);
|
||||
}
|
||||
#endregion
|
||||
static double _round_tick(double tick_unit_frac, double input) =>
|
||||
input - Math.IEEERemainder(input, tick_unit_frac);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user