0

我有五星级和平均分。

  1. 平均分是浮点数
  2. 五星级只有 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 多行代码,但是你可以用一行代码来解决我的长代码。

感谢大家帮助我,对于不清楚的问题和糟糕的英语感到抱歉

4

3 回答 3

3

如果我正确理解您的问题,您想要取平均分并将其转换为星级评分,其中每颗星值两分,并且您开始时似乎是和average之间的小数05

如果是这样:

// Flags for whether to show stars, based on `average` being
// a fractional number `0 <= average < 5`
var stars = [
    average > 0,
    average > 1,
    average > 2,
    average > 3,
    average > 4
];

那些是旗帜。用于stars[0]决定是否显示第一颗星,stars[1]决定是否显示第二颗等。

如果您average实际上介于0和之间10,则只需相应地调整数字:

// Flags for whether to show stars, based on `average` being
// a fractional number `0 <= average < 10`
var stars = [
    average > 0,
    average > 2,
    average > 4,
    average > 6,
    average > 8
];
于 2013-01-30T07:27:51.270 回答
1

我想你只需要

Math.floor(average * 2.0) / 2.0;

此代码仅从任何浮点数中留下 xy。

编辑:我犯了一个小错误。我将它设置为 0.1 步,所以它会使 0.85 到 0.8,现在固定 (1/2.0) = 0.5 步。您可以将 4.0 用于 0.25 步,将 10.0 用于 0.1,依此类推。

于 2013-01-30T07:31:02.927 回答
0
function getAverage(twofixed)
        {
            var average;
            if(twofixed[1] == undefined)
            {
                average = twofixed[0];
            }
            else
            {
                average = (twofixed[1] <= 99) ? twofixed[0]+0.50 : twofixed[0]+1;
            }
            return average;
        }
于 2013-01-30T07:32:31.490 回答