Question: Say I have a condition that if a number ends with.00 add 2 to it and if a number ends with.99 add 3 to it. So if I had 5.00 and 5.99, I want to write a nested if statement that would allow me to add the right value to the original number based on its ending.

How to do that in R (or python)?

Answer: 
Due to [floating point precision](https://stackoverflow.com/questions/588004/is-floating-point-math-broken), you will need to round your numbers to two decimals as values such as `5.99` cannot be represented exactly. This means a value such as `5.9921875` will have to fulfill your criterion as *finishing with 0.99*.

If you are fine with that, using [Python's Format Specification Mini-Language](https://docs.python.org/3/library/string.html#format-specification-mini-language) 
 will round implicitly and allow to extract the required decimals.

```
def get_decimals(num, n=2):
    return '{0:.2f}'.format(num)[-2:]

def func(num):
    decs = get_decimals(num)

    if decs == '00':
        return num + 2
    elif decs == '99':
        return num + 3
    else: return num

```
In `python`.

```
def func(num):
    if round(num, 2) - int(num) < 0.001:
        return num + 2
    elif round(num, 2) - int(num) > 0.989:
        return num + 3 
    return num

```

You can round the number to 2 decimal places, subtract it's integer component, then round the result to check if it is *close to* `0.00` or `0.99`. Simply checking if the result before rounding is 0.00 or 0.99 is unreliable because of [`floating point numbers`](https://en.wikipedia.org/wiki/Floating-point_arithmetic).