Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to trim a float number?
#1
Does anyone know how to trim / clip a float number?

I'm printing out a lot of numbers to my shell window and would like to trim float numbers. I had a quick look at the built in routines but can't remember how to do it. I created a little routine and was wondering if this was the best way to get it done. I'm hoping someone has a better way.

def mytrim(float_number, length =2):
    if isinstance(float_number, float):
        num =str(float_number)
        digits =num.split(".")
        return_str =digits[0] +"." +str(digits[1])[:length]

        return return_str
# mytrim() ------------------------------------------------------------------
    
print(mytrim(328.88452), mytrim(31.11547999999999))
Output:
328.88 31.11
Reply
#2
String formatting?
>>> some_num = 328.88452
>>> some_other_num = 31.11547999999999
>>> print("{:.2f}, {:.2f}".format(some_num, some_other_num))
328.88, 31.12
>>>
Reply
#3
Thanks Mekire, that's just what I was looking for.
Reply
#4
There is also format() option:

>>> format(31.11547999999999, '.2f')
'31.12'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(Aug-20-2018, 07:49 PM)perfringo Wrote: There is also format() option:
Yes,but is more rarely used.

I use f-string in all do since 3.6 came out.
Use format() sometime on forum,because not all use 3.6 or 3.7.
>>> some_num = 328.88452
>>> some_other_num = 31.11547999999999
>>> print(f"{some_num:.2f}, {some_other_num:.2f}")
328.88, 31.12
Reply
#6
(Aug-20-2018, 08:22 PM)snippsat Wrote:
(Aug-20-2018, 07:49 PM)perfringo Wrote: There is also format() option:
Yes,but is more rarely used.

It may be so. However, OP-s question was not about rarity or ordinarity. It's just one way of doing it.

There is "that's just what I was looking for" from OP, however it's fair to mention that no solution provided actually meets initial requirements. Example output is 31.11 but .2f in whichever flavour returns 31.12.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting float number output barryjo 2 879 May-04-2023, 02:04 PM
Last Post: barryjo
  float("{:.2f}" return long number? korenron 2 989 Jul-13-2022, 05:35 AM
Last Post: korenron
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,763 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 3,324 Jul-25-2019, 11:32 PM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020