+ 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❤️ ,
30 Antworten
+ 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
+ 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
+ 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
+ 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
+ 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
😁😁😁😁😁😁😁😁😱
+ 4
hamishhr
but this isn't a calculator analogy he just need to use int() why does he make it harder for himself
+ 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
+ 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
+ 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)
+ 3
Instead of converting you could also use formatting.
+ 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];
+ 3
Not Python, not sorry 🤭
https://code.sololearn.com/c9N629Y6CwEj/?ref=app
Update:
https://code.sololearn.com/ce3akJx8w2Yk/?ref=app
+ 3
num = input()
if num[len(num)-2,len(num)]== “.0”
num= num[0,len(num)-2]
print(num)
+ 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.
+ 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.
+ 2
i guess you have to use int() after all
+ 2
That code I write needs both int and float not just int
+ 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 !
+ 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.
+ 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...