2

I want to be able to retrieve a number value from a cookie but limit the number to 2 decimal places. The value is stored in a cookie as a result of a change function that calculates, dynamically, a price given preset variable values. Sometimes that number displays as 34.000012, for example. I store the value in a cookie using this:

        $(function () {
        $("#btnCookie").bind("click", function () {
            $.cookie("name", $("#txtName").val());
            $.cookie("amount", $("#txtAmount").val());

I retrieve the value using this:

         <b>Amount:</b> $" + $.cookie("amount")

Since it's supposed to be a price, I'd like it to display only two values after the decimal. How can I accomplish this?

EDIT: This question is different than the alleged duplicate because the value is the result of a change function that calculates price in dollars, the user below has posted the answer I've been seeking. Thanks!

BlueRiver89
  • 77
  • 1
  • 10

1 Answers1

2

The following should do it:

"....... <b>Amount:</b> $" + (+$.cookie("amount")).toFixed(2);
PeterKA
  • 24,158
  • 5
  • 26
  • 48
  • Thank you, this was the answer I needed! – BlueRiver89 Jun 30 '14 at 20:07
  • Just an upadate: I used this, divided the result by the original preset variable, and it is only off by .0001 cents. So, for everything I can possibly imagine doing with what I'm working on, this is perfect! – BlueRiver89 Jun 30 '14 at 20:34