Conditional If statements with decimal constaints

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Conditional If statements with decimal constaints



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.



could someone please help me out on how to do that in R (or python)





what do you mean by right value? so do you need 7.00 and 8.99 as your answer?
– Onyambu
Aug 10 at 16:01





What form is your number in? Note that if it's a float, your representation is not guaranteed to have the exact value that you expect.
– Prune
Aug 10 at 16:05





@onyambu, by "right value" I meant adding the corresponding value, so either the 2 or the 3 based on the ending of the original number.
– Ef. O
Aug 10 at 16:20




4 Answers
4



Due to floating point precision, 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.


5.99


5.9921875



If you are fine with that, using Python's 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





This is great, thank you. Any idea how i can do something similar in R?
– Ef. O
Aug 10 at 16:24





@Oliver nice use of f strings
– modesitt
Aug 10 at 16:26





@Ef.O Unfortunately I have little if any knowledge of R.
– Olivier Melançon
Aug 10 at 16:27





@modesitt This is not actually using an f-string. I wanted to, but decided to keep it simple as it is not a feature commonly used yet.
– Olivier Melançon
Aug 10 at 16:27





No problem will figure it out. Thank you so much for your response, it's really helpful. - @OlivierMelançon
– Ef. O
Aug 10 at 16:32



In python.


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.


0.00


0.99


floating point numbers





Wouldn't you want to check against 0.005 instead of 0.001 and similarly for the second condition?
– Olivier Melançon
Aug 10 at 16:16





I think because the float is reasonable up to 12 digits, these boundaries are tighter but sufficient @OlivierMelançon
– modesitt
Aug 10 at 16:18





I toyed with your code and probably misread it at first because it actually works like a charm. Forget my previous comment
– Olivier Melançon
Aug 10 at 16:26



In R we can do


R


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


v1 <- c(5, 5.99)



It seems there are other values in your list/vector you need to deal with. I will give a vectorized format:



in R:


data = c(5.00,5.99,5.61)
mm = function(x)
x = round(x,2)
y = x-as.integer(x)
ifelse(round(y-0.00,2)==0, x+2,ifelse(round(y-0.99,2)==0,x+3,x))


mm(data)
[1] 7.00 8.99 5.61



In Python: vectorized format with numpy module


numpy module


import numpy as np
x = np.array([5.00,5.99,5.61])
def mm(x):
x = np.round(x,2)
y = x - np.array(x,dtype = 'i')
return np.where(np.round(y-0.00,2)==0, x+2,np.where(np.round(y-0.99,2)==0,x+3,x))

mm(x)
array([7. , 8.99, 5.61])





I think Op is implying that the number should not be changed if the the number was 5.98 for example. my original solution did this too.
– modesitt
Aug 10 at 16:07





@modesitt what exactly do you mean?
– Onyambu
Aug 10 at 16:12






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard