我正在解决玩具问题"我应该编写一个JavaScript函数,将小数转换为分数并将其作为字符串返回。例如:fractionConverter(2.75)应返回" 11/4"。
这是我的代码:
function fractionConverter (number) {
if (number > 0) {
var isNegative = false;
} else if (number < 0) {
var isNegative = true;
}
number = Math.abs(number);
if (number % 1 === 0) {
var finalFrac = number + "/1";
} else {
for (var i = 2; i < 10000000000; i++) {
if ((i * number) % 1 === 0) {
var finalFrac = (i * number) + "/" + i;
}
if (finalFrac) { break; }
}
}
var getFrac = function(numString, bool) {
if (!bool) {
return numString;
} else {
return "-" + numString;
}
}
return getFrac(finalFrac, isNegative);
}
抱歉格式化。无论如何,我得到一个奇怪的规格失败。该函数返回以下数字的正确值:0.5,3,2.5,2.75,-1.75和.88。然而,出于某种原因,它在0.253213上失败了。它返回1266065/5000000而不是253213/1000000。不太清楚为什么。
由于
答案 0 :(得分:4)
我正在改善@ william的回答, 我认为这个脚本可以让你减少分数。
function fractionConverter(number) {
var fraction = number - Math.floor(number);
var precision = Math.pow(10, /\d*$/.exec(new String(number))[0].length);
var getGreatestCommonDivisor = function(fraction, precision) {
if (!precision)
return fraction;
return getGreatestCommonDivisor(precision, fraction % precision);
}
var greatestCommonDivisor = getGreatestCommonDivisor(Math.round(fraction * precision), precision);
var denominator = precision / getGreatestCommonDivisor(Math.round(fraction * precision), precision);
var numerator = Math.round(fraction * precision) / greatestCommonDivisor;
function reduce (numer,denom) {
for (var i = 2; i >= 9; i++) {
if ((numer%i===0) && (denom%i)===0) {
numerator=numer/i;
denominator=denom/i;
reduce(numerator,denominator);
};
};
}
reduce(numerator,denominator);
return numerator + "/" + denominator;
}
document.getElementById("output").innerHTML = fractionConverter(0.24888);
这是HTML
<body>
<p id="output"></p>
</body>
</html>
答案 1 :(得分:2)
Javascript不能准确处理浮点数。
我尝试将其输入节点:
0.253213 * 1000000
我得到了这个:
253213.00000000003
以下是测试乘数的另一种方法
var bigNumber = Math.pow(10,8);
var isDivisible = (Math.round(i * number * bigNumber)/bigNumber % 1) == 0;
这对你有所帮助。
如果您希望0.333333333被视为1/3,这也可以按您预期的方式工作。
一个问题是你可以拥有的最高整数是javascript介于10 ^ 15和10 ^ 16之间。
如果((number * bigNumber)> 2 ^ 53)这将无效。
答案 2 :(得分:1)
我完全改变了代码的结构,但这个解决方案确实有效。它基于this线程的代码。我希望这会有所帮助。
function fractionConverter(number) {
var fraction = number - Math.floor(number);
var precision = Math.pow(10, /\d*$/.exec(new String(number))[0].length);
var getGreatestCommonDivisor = function(fraction, precision) {
if (!precision)
return fraction;
return getGreatestCommonDivisor(precision, fraction % precision);
}
var greatestCommonDivisor = getGreatestCommonDivisor(Math.round(fraction * precision), precision);
var denominator = precision / greatestCommonDivisor;
var numerator = Math.round(fraction * precision) / greatestCommonDivisor;
return numerator + "/" + denominator;
}
document.getElementById("output").innerHTML = fractionConverter(0.253213);
<!DOCTYPE html>
<html>
<body>
<p id="output"></p>
</body>
</html>
答案 3 :(得分:1)
这个答案的警告是ECMAscript不能很好地处理小数。 另请注意,以下内容主要是伪代码,但应使用小修复。
以下是此问题的JavaScript解决方案:
var decimal_to_fraction = {
"numerator": 0,
"denominator": 0,
"simplified_numerator": this.numerator,
"simplified_denominator": this.denominator,
"init": function(numerator, denominator){
this.numerator = numerator
this.denominator = denominator
},
"get_divisor": function(numerator, denominator){
var divisor = 0;
var divisors = [1, 2, 3, 4, 5];
for (i in divisors) {
if (!(numerator % divisor) && !(denominator % divisor)) {
divisor = i;
break
}
}
return divisor
},
"calculate_fraction": function() {
var simplified = false;
divisor = this.get_divisor(numerator_denominator);
if (divisor) {
while (simplified == false) {
if (this.simplfieid_numerator / divisor and this.simplified_denominator / divisor) {
this.simplified_numerator = simplified_numerator / divisor
this.simplified_denominator = simplified_denominator / divisor
} else {
simplified = true
}
}
}
return (this.simplified_numerator, this.simplfieid_denominator)
},
"get_fraction": function() {
this.calculate_fraction()
fraction = "{0} / {1}".format(this.simplfieid_numerator, this.simplified_denominator"
return fraction
}
}
decimal_to_fraction.get_fraction()
如果您感到好奇,这是您解决问题的Python解决方案:
class DecimalToFraction(object):
def __init__(decimal):
self.numerator = decimal * 100
self.denominator = 100
self.simplified_numerator = self.numerator
self.simplified_denominator = self.denominator
def get_divisor(self, numerator, denominator):
divisor = 0
for i in range(0,5):
if not numerator % divisor and not denominator % divisor:
divisor = i
break
return divisor
def calculate_fraction(self):
simplified = False
divisor = get_divisor(self.numerator, self.denominator)
if divisor:
while simplified == False:
if self.simplified_numerator / divisor and self.simplfieid_denominator / divisor:
self.simplified_numerator = simplified_numerator / divisor
self.simplified_denominator = simplified_denominator / divisor
else:
simplified = True
return (self.simplified_numerator, self.simplified_denominator)
def get_fraction(self):
self.calculate_fraction()
fraction = "{0} / {1}".format(self.simplified_numerator, self.simplified_denominator)
return fraction
#d2f = DecimalToFraction(<decimal>)
#d2f.get_fraction()
答案 4 :(得分:0)
您可以使用Erik Garrison的fraction.js库来执行此操作以及更多小数操作。
要做1.75,你可以做到
var f = new Fraction(1.75);
console.log(f.toFraction()); // Results "1 3/4"
console.log(f.s * f.n + " / " + f.d); // Results "7 / 4"
console.log(f.toString()); // Results "1.75