fractions

How to convert decimal to fractions?

吃可爱长大的小学妹 提交于 2019-11-26 23:28:28
问题 What I need to convert decimal to fractions. It is easy to convert to 10's feet. 1.5 => 15/10 This can do via this code: public class Rational { private int num, denom; public Rational(double d) { String s = String.valueOf(d); int digitsDec = s.length() - 1 - s.indexOf('.'); int denom = 1; for (int i = 0; i < digitsDec; i++) { d *= 10; denom *= 10; } int num = (int) Math.round(d); this.num = num; this.denom = denom; } public Rational(int num, int denom) { this.num = num; this.denom = denom; }

How to write fraction value using html?

家住魔仙堡 提交于 2019-11-26 22:20:21
I want to write fraction value such as the picture below: How do I write fraction value using html without using image? NOTE: I don't want this 1 1/2 pattern but strictly just as the pic above thomson_matt Try the following: 1<sup>1</sup>⁄<sub>2</sub> This displays as: 1 1 ⁄ 2 lokesh .frac { display: inline-block; position: relative; vertical-align: middle; letter-spacing: 0.001em; text-align: center; } .frac > span { display: block; padding: 0.1em; } .frac span.bottom { border-top: thin solid black; } .frac span.symbol { display: none; } 1 <div class="frac"> <span>1</span> <span class="symbol

Convert a decimal number to a fraction / rational number

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 16:50:51
问题 In JavaScript, is there any way to convert a decimal number (such as 0.0002 ) to a fraction represented as a string (such as " 2/10000" )? If a function called decimalToFraction had been written for this purpose, then decimalToFraction(0.0002) would return the string "2/10000" . 回答1: You can use Erik Garrison's fraction.js library to do that and more fractional operations. var f = new Fraction(2, 10000); console.log(f.numerator + '/' + f.denominator); To to do .003 you can just do var f = new

Decimal to Fraction conversion in Swift

放肆的年华 提交于 2019-11-26 16:46:00
问题 I am building a calculator and want it to automatically convert every decimal into a fraction. So if the user calculates an expression for which the answer is "0.333333...", it would return "1/3". For "0.25" it would return "1/4". Using GCD, as found here (Decimal to fraction conversion), I have figured out how to convert any rational, terminating decimal into a decimal, but this does not work on any decimal that repeats (like .333333). Every other function for this on stack overflow is in

Is there a javascript function that reduces a fraction

ぐ巨炮叔叔 提交于 2019-11-26 15:35:22
问题 say we have fraction 2/4, it can be reduced to 1/2. Is there javascript function that can do the reducing? 回答1: // Reduce a fraction by finding the Greatest Common Divisor and dividing by it. function reduce(numerator,denominator){ var gcd = function gcd(a,b){ return b ? gcd(b, a%b) : a; }; gcd = gcd(numerator,denominator); return [numerator/gcd, denominator/gcd]; } reduce(2,4); // [1,2] reduce(13427,3413358); // [463,117702] 回答2: No, but you can write one yourself fairly easily. Essentially

range() for floats

江枫思渺然 提交于 2019-11-26 12:13:41
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 kichik 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, 100, 0.1))[-1] 99.9999999999986 To get the expected result, you can use one of the other answers in

How to convert a decimal number into fraction?

笑着哭i 提交于 2019-11-26 12:03:30
问题 I was wondering how to convert a decimal into a fraction in its lowest form in Python. For example: 0.25 -> 1/4 0.5 -> 1/2 1.25 -> 5/4 3 -> 3/1 回答1: You have two options: Use float.as_integer_ratio(): >>> (0.25).as_integer_ratio() (1, 4) (as of Python 3.6, you can do the same with a decimal.Decimal() object.) Use the fractions.Fraction() type: >>> from fractions import Fraction >>> Fraction(0.25) Fraction(1, 4) The latter has a very helpful str() conversion: >>> str(Fraction(0.25)) '1/4' >>>

Best way to represent a fraction in Java?

瘦欲@ 提交于 2019-11-26 11:40:00
I'm trying to work with fractions in Java. I want to implement arithmetic functions. For this, I will first require a way to normalize the functions. I know I can't add 1/6 and 1/2 until I have a common denominator. I will have to add 1/6 and 3/6. A naive approach would have me add 2/12 and 6/12 and then reduce. How can I achieve a common denominator with the least performance penalty? What algorithm is best for this? Version 8 (thanks to hstoerr ): Improvements include: the equals() method is now consistent with the compareTo() method final class Fraction extends Number { private int

Convert a character vector of mixed numbers, fractions, and integers to numeric

女生的网名这么多〃 提交于 2019-11-26 11:34:55
问题 I\'m trying to write an R function to convert fractions and mixed numbers to decimals. e.g. mixedToFloat <- function(x){ x <- sub(\' \', \'+\', x, fixed=TRUE) return(unlist(lapply(x, function(x) eval(parse(text=x))))) } > mixedToFloat(c(\'1 1/2\', \'2 3/4\', \'2/3\', \'11 1/4\', \'1\')) [1] 1.5000000 2.7500000 0.6666667 11.2500000 1.0000000 This works for most of the cases I can think of, but feels a little bit hackish. Is there a more standard way to do this? 回答1: Anything less "hackish"

How to rendering fraction in Swing JComponents

谁说胖子不能爱 提交于 2019-11-26 10:01:45
问题 Basically Swing JComponents are able to display numbers in fractions in this form 2 2/3 . How can I paint fraction in the nicest form, for example 2⅔? . EDIT . as see I have only one way JTable inside JSpinner with one TableColumn and TableRow (that could simulated plain JtextField too), where TableRenderer could be some of JTextComponent formatted by using Html and on TableCellEdit event the TableEditor to swith to the plain JFormattedTextField , is there another way, could it be possible