Richard W - 22 days ago 10

Python Question

I'm looking for, a hopefully simple way to find, the least significant digit in a number (float or integer).

For example:

101 --> 1

101.2 --> 0.2

1.1003 --> 0.0003

Cases like 10, 100, 1000 etc are slightly less relevant, though I'd be interested regardless.

I don't particularly care about the format so if it makes more sense to identify the result in a more practical manner feel free.

For example:

101.2 --> 0.1 or 10^-1 (or even -1 for the relevant power/exponent) is fine.

For additional context, what I am trying to do:

Lets say I have a number 31.6 I want to be able to turn that in into 31.5 (decrease the least significant digit by 1). Lets say I have the number 16, I want to turn that into 15 (same idea). If I have the number 100, I still want to turn that into 99 but I can probably figure out the case handling there (without specifying 100.0, I think the 1 is the least significant digit and decreasing by 1 gives you 0, which is not what I want). This in turn is related to manually manipulating class breaks in data ranges (going off topic though).

Showing my homework:

This looks promising (the leastSigDigit part), but it returns the digit without any (mathematical) reference to the power of the digit (and I'm hoping to avoid lots of string conversions too).

This was what came up when I searched stackoverflow but doesn't seem helpful (if I'm using the wrong terminology/search phrases my apologies in advance)

Answer Source

If you already have floats, this isn't going to work. Python floats don't carry the information you need. There's no distinction between `1.`

and `1.0`

, and if you divide a `1.0`

and a `3.0`

, you get `0.3333333333333333`

(except it's really `0.333333333333333314829616256247390992939472198486328125`

).

If your inputs are strings, you can do it pretty easily:

```
def least_significant_digit_power(num_string):
if '.' in num_string:
# There's a decimal point. Figure out how many digits are to the right
# of the decimal point and negate that.
return -len(num_string.partition('.')[2])
else:
# No decimal point. Count trailing zeros.
return len(num_string) - len(num_string.rstrip('0'))
```