Suppose that we have a System.Decimal number.
For illustration, let's take one whose ToString() representation is as follows:
d.ToString() = "123.4500"
The following can be said about this Decimal. For our purposes here, scale is defined as the number of digits to the right of the decimal point. Effective scale is similar but ignores any trailing zeros that occur in the fractional part. (In other words, these parameters are defined like SQL decimals plus some additional parameters to account for the System.Decimal concept of trailing zeros in the fractional part.)
- Precision: 7
- Scale: 4
- EffectivePrecision: 5
- EffectiveScale: 2
Given an arbitrary System.Decimal, how can I compute all four of these parameters efficiently and without converting to a String and examining the String? The solution probably requires Decimal.GetBits.
Some more examples:
Examples Precision Scale EffectivePrecision EffectiveScale
0 1 (?) 0 1 (?) 0
0.0 2 (?) 1 1 (?) 0
12.45 4 2 4 2
12.4500 6 4 4 2
770 3 0 3 0
(?) Alternatively interpreting these precisions as zero would be fine.