+ 4

Is there a way to get rid of useless zeros in bools in Python?

Is there a way to get rid of useless zeros in bools in Python? Otherwise I still have to use my own functions. Example: 1.500000 or 42.0000000 https://code.sololearn.com/cqtT5yT3xnJN/?ref=app

11th Sep 2022, 6:14 AM
Arda Gurses
Arda Gurses - avatar
17 Réponses
+ 6
print(f"Output:{inp.rstrip('0').rstrip('.') or 0.0}")
11th Sep 2022, 6:51 AM
Oma Falk
Oma Falk - avatar
+ 6
Which "bools"? Aren't you rather trying to format floats? They aren't useless, that's how Python represents the numbers. In case you want to use them for further calculations, I would not recommend you to modify them unless you are 100% sure. If it is about representing output, you could just use rounding or string formatting options.
11th Sep 2022, 6:26 AM
Lisa
Lisa - avatar
+ 6
Arda Gurses , Oma Falk , if input is not a float but an int like 9900, the result will be: 99. such inputs can be happen by accident. so it is necessary to check if input is a float or not.
11th Sep 2022, 11:31 AM
Lothar
Lothar - avatar
+ 5
Arda Gurses , here are some thoughts from me: (1) print() will remove all 'useless zeros' except it is the only one of them: print(12.23000) prints: 12.23 print(12.00000) prints: 12.0 print(0.0) prints: 0.0 (2) for all the cases that should have no 'useless zero': print(f'{12.00000:g}') prints: 12 print(f'{0.00000:g}') prints: 0 so we can use f-string for all cases by using the format specifier `g` to get the task done. this does also solve the issue when printing e.g.100 print(f'{100:g}') prints: 100
11th Sep 2022, 6:36 PM
Lothar
Lothar - avatar
+ 5
Jayakrishna🇮🇳 , we can convert string representation of a float to real float datatype. this can be done inside the f-string output: # if `numbers` are given as string format numbers = ['0.0', '123.1200', '333', '1200', '0', '-17.01'] for inp in numbers: print(f'{float(inp):g}')
12th Sep 2022, 10:25 AM
Lothar
Lothar - avatar
+ 5
Jayakrishna🇮🇳 , maybe there is a misunderstanding. in the initial code the op asked to try it with the following values: lst = ['1.00', '32.230000', '0.5', '123.0000'] for num in lst: print( int(float(num))) # your suggested code this is the result: original value after converting with your sample: print( int(float(num))) # your suggested code 1.00 1 => ok 32.230000 32 => NOT ok, should be 32.23 0.5 0 => NOT ok should be 0.5 123.0000 123 => ok
12th Sep 2022, 12:23 PM
Lothar
Lothar - avatar
+ 5
Prince Kumar Floating point rounding may not always accurate. So That's may because rounding issue, depends on method used for rounding even or odd .. Hope this explain more details : https://www.theregister.com/2006/08/12/floating_point_approximation/
13th Sep 2022, 10:52 AM
Jayakrishna 🇮🇳
+ 5
Prince Kumar , if precision matters, it is better to use Decimal format. see the sample in the file. the sample takes the last number from your photo: https://code.sololearn.com/c5QzNuk5m0qr/?ref=app
13th Sep 2022, 11:04 AM
Lothar
Lothar - avatar
+ 3
Lothar Yes. Ok. I may got diverted by examples... I mean, I will use : inp = float(input()) if inp == int(inp) : inp = int(inp) print(f"Your input: {inp}") Or simply your way : print( f"{float(input()):g}") note : all way, using float(), that's what my suggestion, question .. is about.
12th Sep 2022, 3:22 PM
Jayakrishna 🇮🇳
+ 2
Lothar yes we have to take it into account. The zeroes must be "useless". THanks for this point.
11th Sep 2022, 5:56 PM
Oma Falk
Oma Falk - avatar
+ 2
Lothar My suggestion is only => float(inp) . But why I added int() , is actually not about OP question.. I forgot why added that. May be it's alternative suitables for your example '9900'. Also, 2nd time only observed from OP code is if arr[-1] = '.' : arr.pop() so i thought to making int value.. may be i should see examples again.. But why whole point is asking about only print( float( inp) ) # just.. why I added int, is out of my mind now. But not my actual answer.. OP function is equal to : float( inp ) On need to 1.0 to 1 , we can add int(). float() returns as 1.0 Edit: Yes. OP code returning 1.00 to 1 In that case, int() helps... May be the reason i added int()
12th Sep 2022, 2:49 PM
Jayakrishna 🇮🇳
+ 2
Prince Kumar , thanks.
13th Sep 2022, 9:55 AM
Lothar
Lothar - avatar
+ 1
Why not float(inp) just? print(int(float(inp)))
11th Sep 2022, 12:12 PM
Jayakrishna 🇮🇳
+ 1
But the OP taking value as string.. Not float number.. value is in 'inp' of str type... Is that works fine?
11th Sep 2022, 7:03 PM
Jayakrishna 🇮🇳
+ 1
Arda Gurses I think an important question is how you intend to use these strings (that look like floats) once you have them? If you're not just prettying up what was read from input for output, then what will the values be used for?
11th Sep 2022, 9:28 PM
Steve
Steve - avatar
+ 1
Lothar Its the same as print( float(inp) ) why fstring then..? The OP can replace his function with just print( int( float(inp) )) If it is about converting str value, then f'{inp:g} not works. If it allowed to use float then float( inp ) is enough. So why it not allowed is what I mean.
12th Sep 2022, 11:40 AM
Jayakrishna 🇮🇳