+ 10

Python question 😭

I want convert this 👉 number = 10.0 👈 to this 👉10👈 but not with (int) or (//) or (round), I want when I output 10.8 see 10.8 but 10.0 to 10 actually I want just remove ( 0 ) not any numbers else , Thanks❤️ ,

30th Jan 2022, 4:53 PM
Mark Ty
Mark Ty - avatar
29 odpowiedzi
+ 12
[UPDATE] Using eval instead of float beacuse using float seems to he unfair to me. Here is my one liner: num = eval(input()); print('{:.0f}'.format(num)) if (num+0.0).is_integer() else print(num) #i have used (num+0.0) because is_integer methods acts on floats ''' 123 123 123.0 123 123.45 123.45 Deals with all the test cases(int or float) ''' https://code.sololearn.com/cynz8ZrIDBu2/?ref=app ———————————— Old: a = input() if a[-1] == ‘0’: print(a[:-2] else: print(a) The above code only works if there is only one digit after the decimal point. Thnx Simon Sauter for suggesting me formating Thnx Lisa for making me aware of is_inetger method
30th Jan 2022, 5:03 PM
hamishhr
hamishhr - avatar
+ 7
Try this code. It'll work. #My Updated Code num=float(input()) ans=str(num).split(".") if ans[1]==str(0): print(ans[0]) else: print(num) OUTPUT >> 12.8 12.8 >> 12.0 12 >> 12.0000000 12 >> 12.000045 12.000045
1st Feb 2022, 5:14 AM
AYUSH
AYUSH - avatar
+ 6
It's those 'Ways to skin a cat' or 'Do things with one hand tied behind my back' type problems. Code Challenge. 😎 Kung Fu Training For Coders OK I added another method using regex because the problem states using int() is cheating and too easy. 😁 The goal of programming is to complicate things and make life harder. https://code.sololearn.com/cie1qTlpjNgt/?ref=app
30th Jan 2022, 11:22 PM
Bob_Li
Bob_Li - avatar
+ 5
num = float(input()); print('{:.0f}'.format(num)) if num.is_integer() else print(num) #i have used float(input()) because is_integer methods on floats ''' 123 123 123.0 123 123.45 123.45 Deals with all the test cases(int or float) ''' https://code.sololearn.com/cynz8ZrIDBu2/?ref=app Simon Sauter updated the code
30th Jan 2022, 7:33 PM
hamishhr
hamishhr - avatar
+ 5
num = input ("Enter num :") if float(num).is_integer(): print( num.split('.')[0]) else: print(num) OUTPUT >>> 10.0 10 >>> 10.6 10.6 😁😁😁😁😁😁😁😁😱
1st Feb 2022, 3:03 AM
Sandeep Jadam
Sandeep Jadam - avatar
+ 4
hamishhr but this isn't a calculator analogy he just need to use int() why does he make it harder for himself
30th Jan 2022, 5:20 PM
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•)
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•) - avatar
+ 3
hamishhr you don't have to use all of that code just to remove the decimal point just use int() it's more make sense
30th Jan 2022, 5:12 PM
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•)
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•) - avatar
+ 3
This uses int(), but I think it does what you're trying to do. (May not be entirely reliable because of floating point precision issues.) https://code.sololearn.com/cwb3RvIvPbpO/?ref=app
30th Jan 2022, 6:30 PM
Simon Sauter
Simon Sauter - avatar
+ 3
# How about the is_integer() method? print(x := 42.0, x.is_integer()) print(y := 42.42, y.is_integer()) # If it returns True, you can safely cast it to int... # edit: No casting, only formating print(x := 42.00, x.is_integer()) print(f"{x:.0f}" if x.is_integer() else x)
30th Jan 2022, 6:48 PM
Lisa
Lisa - avatar
+ 3
Instead of converting you could also use formatting.
30th Jan 2022, 7:37 PM
Simon Sauter
Simon Sauter - avatar
+ 3
Forgive my psuedocode & C syntax: //Convert characters to a string str[], n=strlen(str); //count the characters // confirm the decimal point is at (n-1), if (str[n-1]=='.'){ if (str[n] != '0') ; // do nothing else n-=2; } for (i=0;i<=n;i++) print str[i];
1st Feb 2022, 2:36 AM
HungryTradie
HungryTradie - avatar
+ 3
num = input() if num[len(num)-2,len(num)]== “.0” num= num[0,len(num)-2] print(num)
1st Feb 2022, 6:05 AM
Devansh rana
Devansh rana - avatar
+ 3
Rebel Raj Younes Hasannejad start a new topic. Go back to the main Q&A Discussions page. the one with the Discuss title and lots of topics. See the button +New Post ? click it and state your problem. Do not click on the little circle + in here. That is for putting in your answer.
1st Feb 2022, 8:00 AM
Bob_Li
Bob_Li - avatar
+ 3
Sandeep Jadam Because ".split ()" is a string function U can't apply it on float dtype. And here we wrote program to convert float into int with certain parameters. So, its nice to take float as input.
1st Feb 2022, 1:36 PM
AYUSH
AYUSH - avatar
+ 2
i guess you have to use int() after all
30th Jan 2022, 5:04 PM
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•)
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•) - avatar
+ 2
That code I write needs both int and float not just int
30th Jan 2022, 5:16 PM
Mark Ty
Mark Ty - avatar
+ 2
Use Map function for type casting: new = map(int, input()) this will convert your float input to int and save it new. you can convert any other variable also just place it at input(). and Boom !
1st Feb 2022, 4:52 AM
Devansh rana
Devansh rana - avatar
+ 2
АлКа Релова , Devansh rana your codes don't achieve the goal. The point is to only get rid of the decimals if they are zeroes. You get rid of all decimals.
1st Feb 2022, 5:19 AM
Simon Sauter
Simon Sauter - avatar
+ 2
Devansh rana the challenge is not to use int() my question is, is the input string or float? Those float solutions are very nice. But float() is ok but int() is not seems unfair...
1st Feb 2022, 5:57 AM
Bob_Li
Bob_Li - avatar