Humanizer.MetricNumeralExtensions.md

August 3, 2026 ยท View on GitHub

MetricNumeralExtensions Class

Contains extension methods for changing a number to Metric representation (ToMetric) and from Metric representation back to the number (FromMetric)

public static class MetricNumeralExtensions

Inheritance System.Object โ†’ MetricNumeralExtensions

Methods

MetricNumeralExtensions.FromMetric(this string) Method

Converts a Metric representation into a number.

public static double FromMetric(this string input);
Parameters

input System.String

Metric representation to convert to a number

Returns

System.Double
A number after a conversion from a Metric representation.

Example
"1k".FromMetric() => 1000d
"123".FromMetric() => 123d
"100m".FromMetric() => 1E-1
Remarks

We don't support input in the format {number}{name} nor {number} {name}. We only provide a solution for {number}{symbol} and {number} {symbol}.

MetricNumeralExtensions.ToMetric(this double, Nullable<MetricNumeralFormats>, Nullable<int>) Method

Converts a number into a valid and Human-readable Metric representation.

public static string ToMetric(this double input, System.Nullable<Humanizer.MetricNumeralFormats> formats=null, System.Nullable<int> decimals=null);
Parameters

input System.Double

Number to convert to a Metric representation.

formats System.Nullable<MetricNumeralFormats>

A bitwise combination of MetricNumeralFormats enumeration values that format the metric representation.

decimals System.Nullable<System.Int32>

The maximum number of fractional digits to include. When KeepTrailingZeros is used, exactly this many fractional digits from 0 through 15 are included. If null, all available precision is preserved and the flag has no effect.

Returns

System.String
A valid Metric representation

Exceptions

System.ArgumentOutOfRangeException
decimals is outside the range 0 through 15 when KeepTrailingZeros is used.

Example
1000d.ToMetric() => "1k"
123d.ToMetric() => "123"
1E-1.ToMetric() => "100m"
Remarks

Inspired by a snippet from Thom Smith. See <a href="http://stackoverflow\.com/questions/12181024/formatting\-a\-number\-with\-a\-metric\-prefix"\>this link</a> for more.

MetricNumeralExtensions.ToMetric(this int, Nullable<MetricNumeralFormats>, Nullable<int>) Method

Converts a number into a valid and Human-readable Metric representation.

public static string ToMetric(this int input, System.Nullable<Humanizer.MetricNumeralFormats> formats=null, System.Nullable<int> decimals=null);
Parameters

input System.Int32

Number to convert to a Metric representation.

formats System.Nullable<MetricNumeralFormats>

A bitwise combination of MetricNumeralFormats enumeration values that format the metric representation.

decimals System.Nullable<System.Int32>

The maximum number of fractional digits to include. When KeepTrailingZeros is used, exactly this many fractional digits from 0 through 15 are included. If null, all available precision is preserved and the flag has no effect.

Returns

System.String
A valid Metric representation

Exceptions

System.ArgumentOutOfRangeException
decimals is outside the range 0 through 15 when KeepTrailingZeros is used.

Example
1000.ToMetric() => "1k"
123.ToMetric() => "123"
1E-1.ToMetric() => "100m"
Remarks

Inspired by a snippet from Thom Smith. See <a href="http://stackoverflow\.com/questions/12181024/formatting\-a\-number\-with\-a\-metric\-prefix"\>this link</a> for more.

MetricNumeralExtensions.ToMetric(this long, Nullable<MetricNumeralFormats>, Nullable<int>) Method

Converts a number into a valid and Human-readable Metric representation.

public static string ToMetric(this long input, System.Nullable<Humanizer.MetricNumeralFormats> formats=null, System.Nullable<int> decimals=null);
Parameters

input System.Int64

Number to convert to a Metric representation.

formats System.Nullable<MetricNumeralFormats>

A bitwise combination of MetricNumeralFormats enumeration values that format the metric representation.

decimals System.Nullable<System.Int32>

The maximum number of fractional digits to include. When KeepTrailingZeros is used, exactly this many fractional digits from 0 through 15 are included. If null, all available precision is preserved and the flag has no effect.

Returns

System.String
A valid Metric representation

Exceptions

System.ArgumentOutOfRangeException
decimals is outside the range 0 through 15 when KeepTrailingZeros is used.

Example
1000.ToMetric() => "1k"
123.ToMetric() => "123"
1E-1.ToMetric() => "100m"
Remarks

Inspired by a snippet from Thom Smith. See <a href="http://stackoverflow\.com/questions/12181024/formatting\-a\-number\-with\-a\-metric\-prefix"\>this link</a> for more.