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
#11
(Oct-20-2022, 05:29 PM)Skaperen Wrote: i want this to be in the decimal.Decimal type which may or may not store the fraction, which can be zero if stored.

sorry, my first post just said "decimal" instead of "decimal.Decimal".

I don't know anything in Decimal that will do this. But the same conversion algorithm would still work. If it is equivalent to the integer, reset it.

>>> l = [decimal.Decimal(f"{d}.0") / decimal.Decimal("2") for d in range(1,11)]
>>> [str(x) for x in l]
['0.5', '1.0', '1.5', '2.0', '2.5', '3.0', '3.5', '4.0', '4.5', '5.0']
>>> l = [decimal.Decimal(int(d)) if d == int(d) else d for d in l]
>>> [str(x) for x in l]
['0.5', '1', '1.5', '2', '2.5', '3', '3.5', '4', '4.5', '5']
Reply
#12
i have coded:
...
if number == int(number)
    number = int(number)
...
just like you suggested above in post #2. i was looking for something i could put in the print() call and eliminate those 2 lines. i always like shorter code as it make it easier to read (and faster to compile on Thursdays).

if i had been the one who implemented decimal, i would have included such a method. i don't know what i would have named it.

edit:

such a method would return the modified value, as Decimal objects are immutable.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#13
i coded this little thing:
from decimal import Decimal
point = str(3/2)[1]

def dec_min_frac(n):
    if isinstance(n,(int,float,str,bytes,bytearray)):
        n = Decimal(n)
    elif isinstance(n,list):
        return [dec_min_frac(x)for x in n]
    elif isinstance(n,tuple):
        return tuple(dec_min_frac(x)for x in n)
    else:
        return None
    s = str(n)
    if point in s:
        while s[-1] == '0':
             s = s[:-1]
        if s[-1] == '.':
             s = s[:-1]
    return Decimal(s)


if __name__ == '__main__':
    from sys import argv
    argv.pop(0)
    print(' '.join(str(x) for x in dec_min_frac(argv)))
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#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
#15
Well, I just looked at decimal module docs.

I think the normalize method is what you need.

In [1]: import decimal

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

In [3]: num.normalize()
Out[3]: Decimal('37')

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

In [5]: num.normalize()
Out[5]: Decimal('37.1')

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

In [7]: num.normalize()
Out[7]: Decimal('37')
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#16
(Oct-21-2022, 09:46 PM)wavic Wrote: I think the normalize method is what you need.
the library manual (PDF version for 3.7) describes conversion to what appears to be an exponential form, e.g. Decimal('0e0'). that's not what i wanted so i skipped further consideration of that method.

edit:

testing shows that the manual seems to be wrong, incomplete, or misleading. it looks like .normalize() may well be useful.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#17
One line after their Decimal('0') to Decimal('0e0') explanation there is an example that says

Quote:Decimal('32.100') and Decimal('0.321000e+2') both normalize to the equivalent value Decimal('32.1').

Both manuals ( 3.7 and 3.10 ) are the same. Perhaps you have been in a hurry.

Morning is cold Dodgy
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#18
(Oct-22-2022, 05:04 AM)wavic Wrote: Both manuals ( 3.7 and 3.10 ) are the same. Perhaps you have been in a hurry.
i have not downloaded any manual since 3.7, which i did when i was running 3.6, taking care to check for changes made as done in 3.7). thus, i have no 3.10 handy to compare with. i use the PDF versions.

maybe i should go download manuals for 3.8, which is the system Python version in {U,Xu}buntu 20.04 LTS which makes use of Python so i am reluctant to consider upgrading to a newer version unless it comes from Canonical.
wavic likes this post
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fraction Calculation with Limitations TINMAN01 13 5,422 Dec-22-2020, 04:45 AM
Last Post: bowlofred
  Single digits seem to be greater than double digits Frosty_GM 4 3,521 Nov-20-2020, 10:13 AM
Last Post: DeaD_EyE
  Printing digits after the decimal point one by one uberman321 1 1,753 Oct-20-2020, 08:10 AM
Last Post: bowlofred
  fraction module: can you stop the reducing? qmfoam 1 2,410 Oct-10-2020, 06:10 PM
Last Post: bowlofred
  remove elements method not working spalisetty06 4 2,488 Aug-13-2020, 01:17 PM
Last Post: deanhystad
  testing for Decimal w/o importing decimal every time Skaperen 7 4,470 May-06-2019, 10:23 PM
Last Post: Skaperen
  whole number and fraction Skaperen 11 5,585 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,187 Apr-26-2018, 07:50 AM
Last Post: Pleiades
  decimal or hexadecimal digits to int Skaperen 3 4,185 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,683 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