Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
whole number and fraction
#1
i want to get the whole value and fraction value of a float value i have. i converted it to string, split that by '.' (maybe it needs to be ','), then converted to ints. is there a better way? getting the whole value looks easy. is there a better way to get the fractional part?
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
There is a fractions module. fractions.Fraction() can take a float when initialized.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
so fractions.Fraction(1.5) will give me 0.5?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
just use float() function if you want to get whole or fraction value as a float. For better understand see the example
# for integers
print(float(10))
# for fraction 
print(float(11.22))
Reply
#5
(Mar-15-2019, 10:51 PM)Skaperen Wrote: so fractions.Fraction(1.5) will give me 0.5?

No. But it's not hard to get there from fractions.Franction(1.5).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(Mar-15-2019, 10:51 PM)Skaperen Wrote: so fractions.Fraction(1.5) will give me 0.5?

Be careful, 1.5 is a string, so it should be defined appropriately, as follows. Cheers
# ----- frac.py ---------
from fractions import Fraction
print (Fraction('1.5'))
# ------ OUTPUT ---------
# C:\Training>python frac.py
# 3/2
# -----------------------
Reply
#7
As it turns out, 1.5 does not have to be defined as a string:

>>> import fractions
>>> x = fractions.Fraction(1.5)
>>> x
Fraction(3, 2)
>>> print(x)
3/2
>>> y = fractions.Fraction(x.numerator % x.denominator, x.denominator)
>>> print(y)
1/2
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
(Mar-16-2019, 03:14 PM)ichabod801 Wrote: As it turns out, 1.5 does not have to be defined as a string:
It depends on your targets, mine are:
# ----- frac2.py ---------
from fractions import Fraction
print (Fraction('1.5'))
print (Fraction('3.14159265358979323846'))   
print (Fraction('3.14159265358979323846').limit_denominator(10000))   
print (Fraction('3.14159265358979323846').limit_denominator(100))   
print (Fraction('3.14159265358979323846').limit_denominator(10))   
print (Fraction(125, 50).numerator)    
print (Fraction(125, 50).denominator) 
#-------- OUTPUT ---------------
# C:\Training>python frac2.py
# 3/2
# 157079632679489661923/50000000000000000000
# 355/113
# 311/99
# 22/7
# 5
# 2
#--------------------------------
Reply
#9
What about divmod()?

>>> a=1.5
>>> divmod(a,1)
(1.0, 0.5)
>>> a=3.14159
>>> divmod(a,1)
(3.0, 0.14158999999999988)
Reply
#10
getting 1.5 from a string is just one way to do it. i could get 1.5 from 3.0/2.0 in python2 or python3 and get 1.5 from 3/2 in python3. there are other ways as well. getting 1 is as simple as int(1.5). getting 0.5 might work by doing 1.5-int(1.5). it did work in one test. i don't know if it will in all cases. maybe 1.5-float(int(1.5)) ensures that it always will.
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
  method to remove zero digits from fraction in decimal Skaperen 17 2,750 Oct-23-2022, 04:02 AM
Last Post: Skaperen
  Fraction Calculation with Limitations TINMAN01 13 5,398 Dec-22-2020, 04:45 AM
Last Post: bowlofred
  fraction module: can you stop the reducing? qmfoam 1 2,404 Oct-10-2020, 06:10 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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