How can I count the scale of a given decimal in Powershell?
$a = 0.0001
$b = 0.000001
Casting $a to a string and returning $a.Length gives a result of 6...I need 4.
I thought there'd be a decimal or math function but I haven't found it and messing with a string seems inelegant.
There's probably a better mathematic way but I'd find the decimal places like this:
Basically, split the string on the
.character and get the length of the last string in the array. Wrapping$ain double-quotes implicitly calls.ToString()with an invariant culture (you could expand this as$a.ToString([CultureInfo]::InvariantCulture)), making this method to determine the number of decimal places culture-invariant..TrimEnd('0')is used in case$awere sourced from a string, not a proper number type, it's possible that trailing zeroes could be included that should not count as decimal places. However, if you want the scale and not just the used decimal places, leave.TrimEnd('0')off like so: