0

I wanted to format the the value 1000 into $1,000, how can I do something like below:

<span>money(1000)</span>

<h2>Book sales chart</h2> 
<script src="//cdnjs.cloudflare.com/ajax/libs/accounting.js/0.3.2/accounting.min.js"></script>

<script>
    $(function () {   
        function money(value) {
            return accounting.formatMoney(value, "$");
         }
    }
</script>
Rajeshwar
  • 2,290
  • 4
  • 31
  • 41
Alvin
  • 8,219
  • 25
  • 96
  • 177
  • Duplicate of [*How can I format numbers as money in JavaScript?*](http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript) – RobG Feb 08 '15 at 08:27

2 Answers2

0

You can do something like this:

 <span id="price"></span>

 <script>
  $(document).ready(function(){
  var p = money(1000);
  document.getElementById("price").innerHTML = p ;  //javascript. or
  $("span#price").text(p);   //jquery
 });
 </script>


 <script>
function moneyformat(mon)
 {
   var money = "$";
   var moneyform = money + mon;
   return moneyform;
  }
 </script>
user254153
  • 1,855
  • 4
  • 41
  • 84
0

Here's a function that takes a number, then outputs a string with formatted dollars:

function formatDollars(n) {
    return "$" + n.toFixed(2).replace(/./g, function(c,i,a) {
        return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c;
    });
}

Use jQuery to grab the value of whatever you need to format, pass the value through the above function, and then update the elements. Good luck :)