UnitsNet:将值渲染为具有单位全名的字符串吗?

问题描述 投票:1回答:1

使用UnitsNet,例如,如果我有一个len值...

var len = Length.FromMiles(60);

...将其呈现为字符串总是以其缩写形式产生度量单位:

len.ToString(); // "60 mi"

是否存在ToString重载,可以产生度量单位的全名?例如,

len.ToString(???); // "60 miles"

FYI,我正在使用程序包的version 4.36.0

c# units-of-measurement
1个回答
1
投票

根据该库的String Formatting文档,Value"v")和Unit('"u")有指定的格式字符串,可用于显示值的组合和完整的单位名称。

根据示例,您可以执行以下操作:

Console.WriteLine("Length is {0:v} {0:u}s", len);

// Output: "Length is 60 Miles"

其他方法:

len.ToString("v") + " " + len.ToString("u") + "s"
// or
$"{len:v} {len:u}s"
// will produce: "60 Miles"

[注意,我们必须调用ToString两次,用空格分隔调用,并且该单元为大写而不是“复数”,可能值得编写扩展方法来帮助格式化。它还将使我们能够处理特殊情况,例如复合词的单位(名称中应带有空格或连字符)以及其复数形式不只是在末尾添加's'的单位:

public static class Extensions
{
    private static readonly Dictionary<LengthUnit, string> CompoundWordUnits =
        new Dictionary<LengthUnit, string>
        {
            {LengthUnit.AstronomicalUnit, "astronomical unit"},
            {LengthUnit.DtpPica, "dtp pica"},
            {LengthUnit.DtpPoint, "dtp point"},
            {LengthUnit.KilolightYear, "kilolight-year"},
            {LengthUnit.LightYear, "light-year"},
            {LengthUnit.MegalightYear, "megalight-year"},
            {LengthUnit.NauticalMile, "nautical mile"},
            {LengthUnit.PrinterPica, "printer pica"},
            {LengthUnit.PrinterPoint, "printer point"},
            {LengthUnit.SolarRadius, "solar radius"},
            {LengthUnit.UsSurveyFoot, "US survey foot"},
        };

    private static readonly Dictionary<LengthUnit, string> SpecialPluralUnits =
        new Dictionary<LengthUnit, string>
        {
            {LengthUnit.Foot, "feet"},
            {LengthUnit.Inch, "inches"},
            {LengthUnit.Microinch, "microinches"},
            {LengthUnit.SolarRadius, "solar radii"},
            {LengthUnit.UsSurveyFoot, "US survey feet"},
        };

    public static string ToGramaticallyCorrectString(this Length length)
    {
        if (length == null) throw new ArgumentNullException(nameof(length));

        // Get the singular form
        var unit = CompoundWordUnits.ContainsKey(length.Unit)
            ? CompoundWordUnits[length.Unit]
            : length.Unit.ToString().ToLower();

        // Get the plural form if needed
        if (length.Value != 1)
            unit = SpecialPluralUnits.ContainsKey(length.Unit)
                ? SpecialPluralUnits[length.Unit]
                : $"{unit}s";

        return $"{length:v} {unit}";
    }
}

有了这个,我们现在可以做:

public static void Main(string[] args)
{
    var lengths = new List<Length>
    {
        Length.FromMiles(1),
        Length.FromMiles(1).ToUnit(LengthUnit.Foot),
        Length.FromMiles(1).ToUnit(LengthUnit.LightYear),
        Length.FromMiles(1).ToUnit(LengthUnit.UsSurveyFoot),
        Length.FromUsSurveyFeet(1),
    };

    lengths.ForEach(length => Console.WriteLine(length.ToGramaticallyCorrectString()));

    GetKeyFromUser("\nDone! Press any key to exit...");
}

输出

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.