I think you should just do what you suggested - use the position of the decimal point. Obvious drawback might be that you have to think about internatialisation yourself.
var decimalSeparator = NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator;
var position = input.IndexOf(decimalSeparator);
var precision = (position == -1) ? 0 : input.Length - position - 1;
// This may be quite unprecise.
var result = Math.Pow(0.1, precision);
There is another thing you could try - the Decimal
type stores a internal precission value. Therefore you could use Deciml.TryParse()
and inspect the returned value. Maybe the parsing algorithm maintains the precission of the input.
Finally I would suggest not to try something using floating point numbers. Just parsing the input will remove any information about trailing zeros. So you have to add an artifical non-zero digit to preserve them or do similar tricks. You might run into precission issues. Finally finding the precission based on a floating point number is not simple, too. I see some ugly math or a loop multiplying with ten every iteration until there is no longer any fractional part. And the loop comes with new precission issues...
UPDATE
Parsing into a deciaml works. Se Decimal.GetBits()
for details.
var input = "123.4560";
var number = Decimal.Parse(input);
// Will be 4.
var precision = (Decimal.GetBits(number)[3] >> 16) & 0x000000FF;
From here using Math.Pow(0.1, precision)
is straight forward.