![]() Find the 'Greatest Common Factor' (the highest number that divides exactly into both the numerator and the denominator).Įxample: Both 45 and 100 are multiples of 5, so we can divide both numbers by 5. Multiply your numerator by 10 / 100 / 1000 (your multiplier from step 1), and then do the same for the denominator. ![]() The denominator below the line is always 1, because a decimal is always part of 1. Write out your decimal as the numerator of a fraction (i.e.This will become your multiplier in step 3. Establish whether your decimal is working in tens, hundreds, thousands or more.If your decimal has three digits after the point, then you are working in thousands, If your decimal has two digits after the point, then you will be working in hundreds. If you're looking at a decimal which only has one number after the point, then you are working in tens. 'thousandth' etc., based on its position after the decimal point. The most important thing you need to keep in mind when you want to convert a decimal to a fraction is that a decimal expresses whether something is a 'tenth', a 'hundredth', a Converting a decimal to a fraction - step by step Some decimals are so familiar to us that we can instantly see them as fractions: if your sister is 14.5 years old, you know that she's 14 1/2 if you buy a bag of potatoes weighingĠ.75kg, you know that it's 3/4 of a kilo if you give your sister a 3/4 kilo bag of potatoes for her 18th birthday, you know that your chances of a polite and enthusiastic response areīut what of other less obvious decimals - how can you calculate what 0.45, 0.62 or 0.384 is as a fraction, for example? Here's how. You can then simplify the fraction if needed. To turn it into a fraction, place the 4 over 10, to give 4/10. As an example, for 0.4 you'll find the four is in Split this off into a separate function to make things a bit nicer: def get_data(message, f = lambda data: data):įunction f is applied to data any exception encounteredĮntry is simply returned if no function is supplied.To convert a decimal to a fraction, take the decimal number and write it as the numerator (top number) over its position value. Print "%d and %d/%d" % ((num//dem), (num%dem),dem) if num//dem !=0 else "%d/%d" % (num%dem,dem)Īs the other answers point out, you can use the integer division and modulo operators to get the numbers you want.Īnother aspect to coding this problem that will make things easier is creating a while loop along with a try, except block for the entry of the improper fraction so that you do not get exceptions. Without using fractions module, we have to find the greatest common divider (borrowing gcd function from fractions) reduce our initial fraction and then use brilliant solution from Felton def gcdm(num,dem): This also gets rid of response of '0 and. Because you feed it num and dem rather than a pure decimal, it is pretty fail-safe. Print str(num // dem) + ' and ' + str(Fraction(num%dem,dem)) if num//dem != 0 else str(Fraction(num%dem,dem)) You use floor divide ( //) to strip out the whole number and then feed the remaining fraction to Fraction: From fractions import Fraction Fraction is nice because it reduces fractions. Here's one solution using Fraction function.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |