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 `R` we can do

```
v1 + (floor(v1) != v1) + 2
#[1] 7.00 8.99

```

---

If we are looking for last two numeric elements

```
v2 <- sub(".*(.{2})$", "\\1", sprintf('%0.2f', v1))
ifelse(v2 == "00", v1 + 2, ifelse(v2 == "99", v1 + 3, v1))
#[1] 7.00 8.99

```

### data

```
v1 <- c(5, 5.99)

```