internal static bool TryParse <TUnit>(string?format, ref int pos, out QuantityFormat <TUnit> result)
            where TUnit : struct, IUnit, IEquatable <TUnit>
        {
            if (string.IsNullOrWhiteSpace(format))
            {
                result = QuantityFormat <TUnit> .Default;
                return(true);
            }

            var valueFormat = DoubleFormatCache.GetOrCreate(format, ref pos);

            var symbolFormat = UnitFormatCache <TUnit> .GetOrCreate(format, ref pos, out var unit);

            if (valueFormat.PostPadding is null &&
                symbolFormat.PrePadding is null)
            {
                // we want to keep the padding specified in the format
                // if both are null QuantityFormat infers padding.
                valueFormat = valueFormat.WithPostPadding(string.Empty);
            }

            if (valueFormat.IsUnknown &&
                symbolFormat.IsUnknown)
            {
                result = QuantityFormat <TUnit> .CreateUnknown(format, Unit <TUnit> .Default);

                return(false);
            }

            if (valueFormat.IsUnknown)
            {
                if (valueFormat.Format is { } &&
Exemple #2
0
        private static QuantityFormat <TUnit> CreateFromValueAndSymbolFormats(ValueAndSymbolFormatKey key)
        {
            var valueFormat = DoubleFormatCache.GetOrCreate(key.ValueFormat);

            var symbolFormat = UnitFormatCache <TUnit> .GetOrCreate(key.SymbolFormat, out TUnit unit);

            return(QuantityFormat <TUnit> .Create(valueFormat, symbolFormat, unit));
        }