-4

I would like the output for each function to yield $x.xx as if it were a price. Being new to javascript is there a simple way to accomplish this or is it a complex system to get the results that I desire for my project?

Below is the code i have made in 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
    here is your answer http://stackoverflow.com/questions/7772699/javascript-format-price – F.C.Hsiao Jan 10 '14 at 02:24
  • @player889: if the question asks for exactly the same thing, you should go and flag the post as a duplicate. Flags bring posts to the attention of higher-reputation users and moderators to ensure that the question gets closed if it shouldn't be open, e.g. it's an exact duplicate, spam, gibberish (a.k.a very low quality), etc. For more information, take a look at [flags](http://stackoverflow.com/help/privileges/flag-posts). – Qantas 94 Heavy Jan 10 '14 at 02:40
  • i didnt know first time using the site – k1773rm0nk3y Jan 10 '14 at 02:51

1 Answers1

0

Use toFixed:

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

Here's the fiddle: http://jsfiddle.net/Uc7x3/

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292