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
#1
when a decimal number is a whole (integral, integer-like) number, it gets converted to string without the ".0". but if i take the result of calculation, such as conversion from Fahrenheit to Celsius, 98.6 gives me 37.0. but when the fraction part is zero, i want to make it like whole. basically i want to have a whole value if the fraction is zero. what decimal method can do this. i looked through the ones in the library manual but i could not find one. must i implement one (i'm sure i can, but this seem silly if there already is one).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Quote:when a decimal number is a whole (integral, integer-like) number, it gets converted to string without the ".0"

I would say the string method of a int doesn't have the decimal, while a float does.

>>> print(int(2))
2
>>> print(float(2))
2.0
You could always convert to an int if appropriate.

if x == int(x):
    x = int(x)
Reply
#3
Not too sure if I fully understand, but here goes:

number = 37.0

print(f"number with the decimal place:    {number}")
print(f"number without the decimal place: {number:.0f}")
Output:
number with the decimal place: 37.0 number without the decimal place: 37
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#4
But if I understand Skaperen right, he only wants to convert a float to an int if the decimal part equals zero.
Reply
#5
(Oct-20-2022, 07:17 AM)ibreeden Wrote: But if I understand Skaperen right, he only wants to convert a float to an int if the decimal part equals zero.

So, more like this, you think?

number = float(input("number: "))

if number % 1 == 0:
    print(f"{int(number)} does not have a decimal place")
else:
    print(f"{number} has a decimal place")
ibreeden likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#6
I don't think you can automatically print a float with no fraction component and have it look like an int. You can specify zero precision, but that prints 1.2 as "1". I cannot find or thing of a way to do this without testing if the value has no fraction component and use different formats based on the result.
def min_float_str(value:float):
    int_val = int(value)
    return str(int_val) if value == int_val else str(value)

print(min_float_str(1.2), min_float_str(1.0))
Output:
1.2 1
Reply
#7
(Oct-20-2022, 05:55 AM)rob101 Wrote: Not too sure if I fully understand, but here goes:

number = 37.0

print(f"number with the decimal place:    {number}")
print(f"number without the decimal place: {number:.0f}")
Output:
number with the decimal place: 37.0 number without the decimal place: 37
that's float. use decimal, like decimal.Decimal(111)/decimal.Decimal(3). and don't add on your own formatting.

i guess i didn't make it clear enough that i am referring to type decimal.Decimal.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
(Oct-20-2022, 04:13 PM)deanhystad Wrote: I don't think you can automatically print a float with no fraction component and have it look like an int.

i meant decimal.Decimal("37") vs decimal.Decimal("37.0").

what i want is a way to have decimal.Decimal("37.0") print as "37" while decimal.Decimal("37.1") prints as "37.1". since decimal.Decimal("37") prints as intended, i was looking for a method like my_decimal_number.remove_fraction_if_zero()
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
(Oct-20-2022, 07:17 AM)ibreeden Wrote: But if I understand Skaperen right, he only wants to convert a float to an int if the decimal part equals zero.
almost.

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".
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
So you want decimal to resize to a minimum precision instead of remembering the precision it inherited from a math operation.
from decimal import Decimal as D

a = D("3")
b = D("2")
c = b / a
d = c * a
print(a, b, c, d)
Output:
3 2 0.6666666666666666666666666667 2.000000000000000000000000000
d should be 2 instead of 2.000000000000000000000000000

One way to make a new decimal with all trailing zeros removed.
from decimal import *
from decimal import Decimal as D

def rstrip(value:D)->D:
    """I'm a happy little function :D with a tragic ending D:"""
    value_str = str(value)
    if "." in value_str:
        return D(value_str.rstrip(".0"))
    return value

a = D("3")
b = D("2")
c = rstrip(b / a)
d = rstrip(c * a)
print(a, b, c, d, D(1000))
Output:
3 2 0.6666666666666666666666666667 2 1000
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,751 Oct-20-2020, 08:10 AM
Last Post: bowlofred
  fraction module: can you stop the reducing? qmfoam 1 2,409 Oct-10-2020, 06:10 PM
Last Post: bowlofred
  remove elements method not working spalisetty06 4 2,487 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,583 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,180 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