feat: port p-rec-annot elevation formatting foundation
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace PRecAnnot.Core;
|
||||
|
||||
public static class LegacyElevationFormatter
|
||||
{
|
||||
public static int RoundModelElevation(double modelZ, double datumOffset)
|
||||
{
|
||||
var increments = Math.Round((modelZ + datumOffset) / 5d, MidpointRounding.AwayFromZero);
|
||||
return checked((int)Math.Round(increments * 5d, MidpointRounding.AwayFromZero));
|
||||
}
|
||||
|
||||
public static string FormatMillimetres(int millimetres)
|
||||
{
|
||||
if (millimetres == 0) return "0,000";
|
||||
var magnitude = Math.Abs((long)millimetres).ToString(CultureInfo.InvariantCulture).PadLeft(4, '0');
|
||||
var metres = magnitude[..^3];
|
||||
var fraction = magnitude[^3..];
|
||||
return $"{(millimetres > 0 ? "+" : "-")}{metres},{fraction}";
|
||||
}
|
||||
|
||||
public static string Calculate(double modelZ, double datumOffset) => FormatMillimetres(RoundModelElevation(modelZ, datumOffset));
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using PRecAnnot.Core;
|
||||
|
||||
namespace PRecAnnot.Core.Tests;
|
||||
|
||||
public sealed class LegacyElevationFormatterTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(0, 0, "0,000")]
|
||||
[InlineData(2.49, 0, "0,000")]
|
||||
[InlineData(2.5, 0, "+0,005")]
|
||||
[InlineData(-2.5, 0, "-0,005")]
|
||||
[InlineData(1233, 2, "+1,235")]
|
||||
[InlineData(-1233, -2, "-1,235")]
|
||||
public void PreservesFiveMillimetreAwayFromZeroLegacyRule(double modelZ, double datum, string expected) =>
|
||||
Assert.Equal(expected, LegacyElevationFormatter.Calculate(modelZ, datum));
|
||||
|
||||
[Theory]
|
||||
[InlineData(5, "+0,005")]
|
||||
[InlineData(50, "+0,050")]
|
||||
[InlineData(500, "+0,500")]
|
||||
[InlineData(1000, "+1,000")]
|
||||
[InlineData(-5, "-0,005")]
|
||||
public void FormatsSignedMetresFromLegacyMillimetreInteger(int value, string expected) =>
|
||||
Assert.Equal(expected, LegacyElevationFormatter.FormatMillimetres(value));
|
||||
}
|
||||
Reference in New Issue
Block a user