你如何在javascript中定价? 1.99美元

时间:2014-01-10 02:19:56

标签: javascript

我希望每个函数的输出产生$ x.xx,就像它是一个价格一样。不熟悉javascript有一个简单的方法可以实现这个目标,还是一个复杂的系统来获得我想要的项目结果?

以下是我在javascript中所做的代码:

    //First function outputs a random number
    var myFirstFunction = function(First)
    {
        return First;

    };

    var FirstAnswer = myFirstFunction(Math.random());


    console.log(FirstAnswer);

    //Second function outputs the random number multiplied by 10
    var mySecondFunction = function(Second)
    {
        return Second * 10;
    };

    var SecondAnswer = mySecondFunction(FirstAnswer);

    console.log(SecondAnswer);

    //Third function divided the random number by 3.3
    var myThirdFunction = function(Third)
    {
        return Third / 3.3;
    }

    var ThirdAnswer = myThirdFunction(SecondAnswer);

    console.log(ThirdAnswer)

1 个答案:

答案 0 :(得分:0)

使用toFixed

function price (number) {
    return '$' + number.toFixed(2);
}

这是小提琴:http://jsfiddle.net/Uc7x3/