Skip to main content
Version: 4.0 (next)

Humanizer.MetricNumeralExtensions

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 are included. If null, all available precision is preserved and the flag has no effect.

Returns

System.String
A valid Metric representation

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 are included. If null, all available precision is preserved and the flag has no effect.

Returns

System.String
A valid Metric representation

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 are included. If null, all available precision is preserved and the flag has no effect.

Returns

System.String
A valid Metric representation

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.