# [JavaScript]JS调用math.js开源库，进行分数的运算

<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/4.0.1/math.min.js"></script>

http://mathjs.org/

s（符号）如果为1表示为正值 -1则表示为负值
n  (分子)
d  (分母)

var fuhao;
if(FuHao[i]==1)
{
fuhao = '+';
Result[i] = Number1[i]+Number2[i];
}
else if(FuHao[i]==0)
{
fuhao = '-';
Result[i] = Number1[i]-Number2[i];
}
else if(FuHao[i]==2)
{
fuhao = 'x';
Result[i] = Number1[i]*Number2[i];
}
else
{
fuhao = "÷";
Result[i] = Number1[i]/Number2[i];
}
text.value = text.value + Number1[i]+" "+fuhao+" "+Number2[i]+" = \n";
}
for(var i=0;i<5;i++)
{
var fuhao;
if(FuHao_FenShu[i]==1)
{
fuhao = '+';
var str = "";
if(temp.s == -1)
{
str = str+"-1";
}
str = str + temp.n +"/" +temp.d;
Result_FenShu[i] = str;
}
else if(FuHao_FenShu[i]==0)
{
fuhao = '-';
var str = "";
var temp = math.subtract(math.fraction(Number_FenShu1[i]),math.fraction(Number_FenShu2[i]));
if(temp.s == -1)
{
str = str+"-1";
}
str = str + temp.n +"/" +temp.d;
Result_FenShu[i] = str;
}
else if(FuHao_FenShu[i]==2)
{
fuhao = 'x';
var str = "";
var temp = math.multiply(math.fraction(Number_FenShu1[i]),math.fraction(Number_FenShu2[i]));
if(temp.s == -1)
{
str = str+"-1";
}
str = str + temp.n +"/" +temp.d;
Result_FenShu[i] = str;
}
else
{
fuhao = '÷';
var str = "";
var temp = math.divide(math.fraction(Number_FenShu1[i]),math.fraction(Number_FenShu2[i]));
if(temp.s == -1)
{
str = str+"-1";
}
str = str + temp.n +"/" +temp.d;
Result_FenShu[i] = str;
}
text.value = text.value + Number_FenShu1[i]+" "+fuhao+" "+Number_FenShu2[i]+" = \n";
}

posted @ 2018-03-22 18:42  leeli73  阅读(1914)  评论(0编辑  收藏  举报