Class DecimalExtensions
Extension methods for System.Decimal.
Inheritance
Namespace: Sartorius.SAF.Extensions
Assembly: Sartorius.SAF.dll
Syntax
public static class DecimalExtensions
Methods
View SourceNormalize(Decimal, Int16, Nullable<MidpointRounding>)
Normalizes the specified value to the number of given numberOfDecimalPlaces
.
Declaration
public static decimal Normalize(this decimal value, short numberOfDecimalPlaces = 0, MidpointRounding? midpointRounding = null)
Parameters
Type | Name | Description |
---|---|---|
System.Decimal | value | The decimal value to normalize. |
System.Int16 | numberOfDecimalPlaces | The number of decimal places. |
System.Nullable<System.MidpointRounding> | midpointRounding | Defines the rounding mode of the value. If no rounding mode is defined, the value is left as it is. |
Returns
Type | Description |
---|---|
System.Decimal | The normalized decimal value. |
Remarks
If a value
has more decimal places than numberOfDecimalPlaces
and no midpointRounding
is defined, the value
is left as it is.
Examples
This example illustrates how to use the Normalize(Decimal, Int16, Nullable<MidpointRounding>) method to normalize a decimal that has two trailing zeroes, to six trailing zero.
// result is 100.000000m
var result = 100.00m.Normalize(6);
This example illustrates how to use the Normalize(Decimal, Int16, Nullable<MidpointRounding>) method to normalize a decimal that has two trailing zeroes, to one trailing zero.
// result is 100.0m
var result = 100.00m.Normalize(1);
This example illustrates how to use the Normalize(Decimal, Int16, Nullable<MidpointRounding>) method to normalize a decimal that has four decimal places, to three decimal places rounded to even.
// result is 100.122m
var result = 100.1225m.Normalize(3, MidpointRounding.ToEven);
This example illustrates how to use the Normalize(Decimal, Int16, Nullable<MidpointRounding>) method to normalize a decimal that has four decimal places, to three decimal places rounded away from zero.
// result is 100.124m
var result = 100.1235m.Normalize(3, MidpointRounding.AwayFromZero);