Skip to main content
Version: 2.11.10

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, bool, bool, Nullable<int>) Method

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

public static string ToMetric(this double input, bool hasSpace, bool useSymbol=true, System.Nullable<int> decimals=null);

Parameters

input System.Double

Number to convert to a Metric representation.

hasSpace System.Boolean

True will split the number and the symbol with a whitespace.

useSymbol System.Boolean

True will use symbol instead of name

decimals System.Nullable<System.Int32>

If not null it is the numbers of decimals to round the number to

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 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>

If not null it is the numbers of decimals to round the number to

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, bool, bool, Nullable<int>) Method

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

public static string ToMetric(this int input, bool hasSpace, bool useSymbol=true, System.Nullable<int> decimals=null);

Parameters

input System.Int32

Number to convert to a Metric representation.

hasSpace System.Boolean

True will split the number and the symbol with a whitespace.

useSymbol System.Boolean

True will use symbol instead of name

decimals System.Nullable<System.Int32>

If not null it is the numbers of decimals to round the number to

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 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>

If not null it is the numbers of decimals to round the number to

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.