Python Forum
method to remove zero digits from fraction in decimal
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
method to remove zero digits from fraction in decimal
#14
In [1]: import math

In [2]: x = lambda num: int(f[1]) if (f := math.modf(num))[0] == 0 else f[0] + f[1]

In [3]: x(4.0)
Out[3]: 4

In [4]: x(4.1)
Out[4]: 4.1
Just noticed you are talking about decimal.Decimal numbers.
It works with those too. Just tested

In [5]: import decimal

In [6]: num = decimal.Decimal('37')

In [7]: x(4.1)
Out[7]: 4.1

In [8]: x(4.0)
Out[8]: 4

In [9]: x(num)
Out[9]: 37

In [10]: num = decimal.Decimal('37.0')

In [11]: x(num)
Out[11]: 37

In [12]: num = decimal.Decimal('37.1')

In [13]: x(num)
Out[13]: 37.1
Ignore in/out 7 and 8. I repeated them by mistake
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
RE: method to remove zero digits from fraction in decimal - by wavic - Oct-21-2022, 09:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Fraction Calculation with Limitations TINMAN01 13 5,885 Dec-22-2020, 04:45 AM
Last Post: bowlofred
  Single digits seem to be greater than double digits Frosty_GM 4 3,768 Nov-20-2020, 10:13 AM
Last Post: DeaD_EyE
  Printing digits after the decimal point one by one uberman321 1 1,875 Oct-20-2020, 08:10 AM
Last Post: bowlofred
  fraction module: can you stop the reducing? qmfoam 1 2,559 Oct-10-2020, 06:10 PM
Last Post: bowlofred
  remove elements method not working spalisetty06 4 2,609 Aug-13-2020, 01:17 PM
Last Post: deanhystad
  testing for Decimal w/o importing decimal every time Skaperen 7 4,747 May-06-2019, 10:23 PM
Last Post: Skaperen
  whole number and fraction Skaperen 11 5,910 Mar-17-2019, 11:33 PM
Last Post: Skaperen
  I'm dividing large numbers of about 25 million digits I need to retain decimal format Pleiades 2 3,334 Apr-26-2018, 07:50 AM
Last Post: Pleiades
  decimal or hexadecimal digits to int Skaperen 3 4,342 Sep-26-2017, 07:03 AM
Last Post: Skaperen
  Input a number with any number of digits and output it in reverse order of digits. sarada2099 6 6,945 Mar-12-2017, 05:45 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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