fractions

convert fraction into string and also insert [] for repeating part

主宰稳场 提交于 2019-12-20 04:33:07
问题 An interview question: Given two int N (numerator) and D (denominator), return the fraction in string. if the fraction is repeating, then display the repeating part in bracket. Example: Input: N=1, D=3 output: 0.[3] Example: Input: N=2, D=5 output: 0.4 My idea: get a = N/D with double value. for part after decimal point, get each digit by x 10 in the process, if find repeating, record the index and insert [] finally. for part before decimal point, get each digit by / 10 Any better ideas?

Fraction object doesn't have __int__ but int(Fraction(…)) still works

☆樱花仙子☆ 提交于 2019-12-19 21:53:10
问题 In Python, when you have an object you can convert it to an integer using the int function. For example int(1.3) will return 1 . This works internally by using the __int__ magic method of the object, in this particular case float.__int__ . In Python Fraction objects can be used to construct exact fractions. from fractions import Fraction x = Fraction(4, 3) Fraction objects lack an __int__ method, but you can still call int() on them and get a sensible integer back. I was wondering how this

How to simplify fractions?

寵の児 提交于 2019-12-19 08:04:25
问题 How to simplify a fraction in C#? For example, given 1 11/6 , I need it simplified to 2 5/6 . 回答1: If all you want is to turn your fraction into a mixed number whose fractional part is a proper fraction like the previous answers assumed, you only need to add numerator / denominator to the whole part of the number and set the numerator to numerator % denominator . Using loops for this is completely unnecessary. However the term "simplify" usually refers to reducing a fraction to its lowest

How to output fraction instead of decimal number?

一曲冷凌霜 提交于 2019-12-17 09:54:12
问题 In C++, When I calculate 2/3, it will output decimal values, how can I just get the original format (i.e.g 2/3) instead of 0.66666667 Thanks 回答1: You can't. You would need to write a class dedicated to holding rational numbers (i.e. fractions). Or maybe just use the Boost Rational Number library. 回答2: If I understand correctly, you have a floating point number (a float or double type variable), and you'd like to output this value as a fraction. If that is the case, you need to further specify

Is there a reliable way in JavaScript to obtain the number of decimal places of an arbitrary number?

拈花ヽ惹草 提交于 2019-12-17 09:37:54
问题 It's important to note that I'm not looking for a rounding function. I am looking for a function that returns the number of decimal places in an arbitrary number's simplified decimal representation. That is, we have the following: decimalPlaces(5555.0); //=> 0 decimalPlaces(5555); //=> 0 decimalPlaces(555.5); //=> 1 decimalPlaces(555.50); //=> 1 decimalPlaces(0.0000005); //=> 7 decimalPlaces(5e-7); //=> 7 decimalPlaces(0.00000055); //=> 8 decimalPlaces(5.5e-7); //=> 8 My first instinct was to

range() for floats

て烟熏妆下的殇ゞ 提交于 2019-12-17 02:21:16
问题 Is there a range() equivalent for floats in Python? >>> range(0.5,5,1.5) [0, 1, 2, 3, 4] >>> range(0.5,5,0.5) Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> range(0.5,5,0.5) ValueError: range() step argument must not be zero 回答1: I don't know a built-in function, but writing one like this shouldn't be too complicated. def frange(x, y, jump): while x < y: yield x x += jump As the comments mention, this could produce unpredictable results like: >>> list(frange(0,

How to represent fractions in html

僤鯓⒐⒋嵵緔 提交于 2019-12-13 20:04:37
问题 I want to represent fractions in html and the code I am using now is this: <sup>3</sup>⁄<sub>8</sub> And it looks like this: But I want to represent it something like this: How do I change it? Need some guidance on this. 回答1: It is not possible through pure HTML. But you can create that type of fraction using a bit of CSS. .fraction { position: relative; width: 1em; font-size: 2em; } .numerator { border-bottom: 2px solid black; text-align: center; } .denominator { border-right: 2px solid

How to get the length of a numbers fraction part?

不打扰是莪最后的温柔 提交于 2019-12-13 14:27:27
问题 How do I find out the length or the number of digits of the fraction part of a decimal number? I can see a few aproaches, e.g. with Strings like this one: public static int getNumberOfFractionDigits(Number number) { Double fractionPart = number.doubleValue() - number.longValue(); return fractionPart.toString().length() - 2; } But what is the best way to determine the length? I could imagine some problems if I use Strings, e.g. because the locale and number format may be different from system

Simplifying Fractions With java

纵饮孤独 提交于 2019-12-13 06:29:15
问题 Hey guys I am working on a SW here I am kinda in need of help, you see we need to make a method where we are gonna simplify fractions. any idea how? here's my code as of now (don't mind the dvalue Method it is already finsih all I need is the simplify method) public class Fraction { public int num; public int den; public double dValue; public void display() { System.out.println("Numerator: "+num); System.out.println("Denominator: "+den); } public double dValue() { dValue = (double)num/den;

Find the smallest integer that when multiplied by a known double, will produce an integer

这一生的挚爱 提交于 2019-12-13 03:49:35
问题 I have a double input "a". My goal is to find an integer "b" such that "a * b" will produce an integer, plus some allowable amount of error. For example "100.227273 * 22 = 2205 (+ 0.000006 error)", where the answer I want to find is "22". I have already studdied this post, but I only partially understand the top answer. I could really use some help creating an algorithm that accomplishes this task. I have some code below that works for some cases, but not all. private int