我有五星级和平均分。
- 平均分是浮点数
- 五星级只有 10 部分,一星有两个半星(所以 10 部分是五星级)
前任:
Average score is: from 0 - 5 (average = 4.658, or average = 0.256, 0 <= average <= 5)
Five star rating is 10 section: [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
如何根据平均分查看平均分并将其设置为10个部分?
这是我的代码,很长。
var twofixed = Math.round(average * 100) / 100;
twofixed = Math.abs(twofixed);
var decimal = twofixed - Math.floor(twofixed)
twofixed = (twofixed + "").split(".");
if(twofixed[0] == 0)
{
if(twofixed[1] == undefined) { average = 0; }
else
{
if(twofixed[1] <= 99)
{
average = 0.50;
}
else
{
average = 1;
}
}
}
else if(twofixed[0] == 1)
{
if(twofixed[1] == undefined) { average = 1; }
else
{
if(twofixed[1] <= 99)
{
average = 1.50;
}
else
{
average = 2;
}
}
}
else if(twofixed[0] == 2)
{
if(twofixed[1] == undefined) { average = 2; }
else
{
if(twofixed[1] <= 99)
{
average = 2.50;
}
else
{
average = 3;
}
}
}
else if(twofixed[0] == 3)
{
if(twofixed[1] == undefined) { average = 3; }
else
{
if(twofixed[1] <= 99)
{
average = 3.50;
}
else
{
average = 4;
}
}
}
else if(twofixed[0] == 4)
{
if(twofixed[1] == undefined) { status = 'Great'; average = 4; }
else
{
if(twofixed[1] <= 99)
{
average = 4.50;
}
else
{
average = 5;
}
}
}
else if(twofixed[0] == 5)
{
if(twofixed[1] == undefined) { average = 5; }
}
非常感谢 @th3falc0n 的小代码,我写了 50 多行代码,但是你可以用一行代码来解决我的长代码。
感谢大家帮助我,对于不清楚的问题和糟糕的英语感到抱歉