-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Rework long.ToMetric for correct interpretation of decimals parameter #1700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
0cb0277
ffaf303
6951f90
616711b
4ffedba
dd29e69
b6242b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -204,7 +204,7 @@ static string BuildDefaultRepresentation(int input) | |
| /// <returns>A valid Metric representation</returns> | ||
| public static string ToMetric(this long input, MetricNumeralFormats? formats = null, int? decimals = null) | ||
| { | ||
| if (input.Equals(0) && (!decimals.HasValue || (decimals == 0))) | ||
| if (input.Equals(0)) | ||
| { | ||
| return input.ToString(); | ||
| } | ||
|
|
@@ -333,11 +333,8 @@ static string BuildRepresentation(long input, MetricNumeralFormats? formats, int | |
| } | ||
|
|
||
| var nfi = LocaleNumberFormattingOverrides.GetFormattingNumberFormat(CultureInfo.CurrentCulture); | ||
| var representation = decimals > 0 | ||
| ? $"{input.ToString(nfi)}{nfi.NumberDecimalSeparator}{new string('0', decimals.Value)}" | ||
| : input.ToString(nfi); | ||
| var space = (formats & MetricNumeralFormats.WithSpace) == MetricNumeralFormats.WithSpace ? " " : string.Empty; | ||
| return representation + space; | ||
| return input.ToString(nfi) + space; | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -373,43 +370,101 @@ static string BuildMetricRepresentation(long input, int scale, MetricNumeralForm | |
| fractionalPart = Math.Abs(input % divisor); | ||
| } | ||
|
|
||
| if (decimals.HasValue) | ||
| { | ||
| for (var i = decimals.Value; i < exponent; i++) | ||
| if (!decimals.HasValue) | ||
| decimals = exponent; | ||
|
|
||
| var unitText = | ||
| Math.Sign(scale) switch | ||
| { | ||
| var roundUp = (i + 1 == exponent); | ||
| +1 => GetUnitText(Symbols[0][scale - 1], formats), | ||
| -1 => GetUnitText(Symbols[1][-scale - 1], formats), | ||
| _ => string.Empty | ||
| }; | ||
|
|
||
| fractionalPart = (fractionalPart + (roundUp ? 5 : 0)) / 10; | ||
| } | ||
| } | ||
| else | ||
| var fractionalPartCharacters = Array.Empty<char>(); | ||
|
|
||
| if (decimals > 0) | ||
| { | ||
| decimals = exponent; | ||
| } | ||
| fractionalPartCharacters = fractionalPart.ToString().PadLeft(exponent, '0').ToCharArray(); | ||
|
|
||
| if (!decimals.HasValue || (decimals >= fractionalPartCharacters.Length)) | ||
| decimals = fractionalPartCharacters.Length; | ||
| else if (fractionalPartCharacters[decimals.Value] >= '5') | ||
| { | ||
| var isExactlyAtMidpoint = | ||
| (fractionalPartCharacters[decimals.Value] == '5') && | ||
| (fractionalPartCharacters.AsSpan().Slice(decimals.Value + 1).IndexOfAnyExcept('0') < 0); | ||
|
|
||
| bool shouldRoundUp; | ||
|
|
||
| if (!isExactlyAtMidpoint) | ||
| shouldRoundUp = true; | ||
| else | ||
| { | ||
| var precedingDigit = fractionalPartCharacters[decimals.Value - 1] - '0'; | ||
|
|
||
| var precedingDigitIsOdd = (precedingDigit & 1) != 0; | ||
|
|
||
| // Banker's rounding: 3.5 => 4, 4.5 => 4 | ||
| shouldRoundUp = precedingDigitIsOdd; | ||
| } | ||
|
|
||
| if (shouldRoundUp) | ||
| { | ||
| // Apply rounding. Find a digit we can increment, carrying as needed. | ||
| for (var i = decimals.Value - 1; i >= 0; i--) | ||
| { | ||
| if (fractionalPartCharacters[i] < '9') | ||
| { | ||
| fractionalPartCharacters[i]++; | ||
| break; | ||
| } | ||
|
|
||
| fractionalPartCharacters[i] = '0'; // loop to carry | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var symbol = Math.Sign(scale) == 1 | ||
| ? Symbols[0][scale - 1] | ||
| : Symbols[1][-scale - 1]; | ||
| while ((decimals > 0) && (fractionalPartCharacters[decimals.Value - 1] == '0')) | ||
| decimals--; | ||
| } | ||
|
|
||
| var nfi = LocaleNumberFormattingOverrides.GetFormattingNumberFormat(CultureInfo.CurrentCulture); | ||
|
|
||
| if (decimals == 0) | ||
| { | ||
| var roundingPoint = divisor / 2; | ||
|
|
||
| if (divisor > 1) | ||
| { | ||
| if (fractionalPart > roundingPoint) | ||
| number += Math.Sign(number); | ||
| else if (fractionalPart == roundingPoint) | ||
| { | ||
| // Use banker's rounding for consistency with Math.Round used elsewhere on floats. | ||
| number += Math.Sign(number % 2); | ||
| } | ||
|
Comment on lines
+440
to
+446
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
After the Useful? React with 👍 / 👎. |
||
|
|
||
| if (Math.Abs(number) == 1000) | ||
| { | ||
| number /= 1000; | ||
| scale++; | ||
| unitText = GetUnitText(Symbols[0][scale - 1], formats); | ||
| } | ||
| } | ||
|
|
||
| var space = formats.HasValue && formats.Value.HasFlag(MetricNumeralFormats.WithSpace) ? " " : string.Empty; | ||
| return number.ToString(nfi) + space + GetUnitText(symbol, formats); | ||
| return number.ToString(nfi) + space + unitText; | ||
| } | ||
| else | ||
| { | ||
| var decimalPlaces = Math.Min(decimals.Value, exponent); | ||
| var extraZeroes = (decimals.Value - decimalPlaces); | ||
| var space = formats.HasValue && formats.Value.HasFlag(MetricNumeralFormats.WithSpace) ? " " : string.Empty; | ||
|
|
||
| return number.ToString(nfi) | ||
| + nfi.NumberDecimalSeparator | ||
| + fractionalPart.ToString("d" + decimalPlaces) | ||
| + (extraZeroes <= 0 ? string.Empty : new string('0', extraZeroes)) | ||
| + new string(fractionalPartCharacters, 0, decimals.Value) | ||
| + space | ||
| + GetUnitText(symbol, formats); | ||
| + unitText; | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.