Search code examples
c#.nethumanizehumanizer

MetricNumeralExtensions.ToMetric is obsolete, use MetricNumeralFormats?


We've upgraded Humanizer from 2.8.26 to 2.11.10 and now get the following warning:

'MetricNumeralExtensions.ToMetric(double, bool, bool, int?)' is obsolete: 'Please use overload with MetricNumeralFormats'

Are there any examples on how to use MetricNumeralFormats? What should I use to make it work?

This is our current code:

using System;
using Humanizer;
                    
public class Program
{
    public static void Main()
    {
        Console.WriteLine(1234.ToMetric(false, true, 2));
    }
}

Try it online

Also asked on GitHub


Solution

  • Here how to use MetricNumeralFormats for your case:

    using System;
    using Humanizer;
    
    public class Program
    {
        public static void Main()
        {
            // instead of
            Console.WriteLine(1234.ToMetric(false, true, 2));
            // do
            var decimals = 2;
            Console.WriteLine(1234.ToMetric(null, decimals));
        }
    }
    

    output

    1.23k
    1.23k
    

    null here is the format your want to use

    For example,

    var decimals = 2;
    var format = MetricNumeralFormats.WithSpace | MetricNumeralFormats.UseName;
    Console.WriteLine(1234.ToMetric(format, decimals)); 
    

    will output

    1.23 kilo
    

    source: MetricNumeralTests and MetricNumeralFormats