Web Analytics Made Easy -
StatCounter Help - Math.round object and disappearing zeros - CodingForum

Announcement

Collapse
No announcement yet.

Help - Math.round object and disappearing zeros

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help - Math.round object and disappearing zeros

    Hi

    wonder if somebody could help me out with this one.

    I'm using the following code:

    function cpu()
    {
    var cqu = "cof.cqr.value";
    var cpu = "cof.cphr.value";
    var amount=Math.round(((eval(cqu))*(eval(cpu)))*100)/100;
    var final = "cof.cpr.value="+amount;
    eval(final);
    }

    Note:cof.cqr, cof.cphr & cof.cpr =the dom path to form elements.

    This takes a quantity value from one text box and * another value with it returning the result and rounding down to two decimal places.

    Problem is that trailing zeros get lost along the way. I need to display them. So 2*1.25 = 2.5 at them moment but I need it to read 2.50. The solution shouldn't upset other value by just adding a 0 on the end whilly nilly. For example 2*1.24=2.48 not 2.480

    I kinda need an answer on this one urgently but is has baffled me. Can anyone help please?

    MattyUK

  • #2
    i think i know how to help you, but i need to see the rest of your page, i think.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

    Comment


    • #3
      sure, here's the link

      Thanks that would be great.

      http://www.tubscorner.co.uk/CodingForum.htm

      You'll have to enter the demo pruice int eh box then go and edit the quantity amount/ the function is called onblur of the quantity text field.

      Thanks for your input.

      MattyUK

      Comment


      • #4
        IE5.5+ and NS6+ both support a toFixed() method of a number object, which returns a string of the number with the decimals to X amount of fixed digits:

        12.325794.toFixed(2) == "12.33"

        jasonkarldavis.com

        Comment


        • #5
          THANK YOU

          That is so cool.

          Thank you very much. I had no idea that was around.

          It'll do me fine.

          Just out of curiosity any idea how it was achieved with older browsers?

          Either way thank you again. You've made my day!

          MattyUK

          Comment


          • #6
            Math.round(4.18239*100)/100 == 4.18

            Hope that helps!

            Happy coding!

            Comment


            • #7
              Cheers nolachrymose

              I'd already got that far. but that method kills trailing zeros. thankfully the toFixed(2) bit can put them back.

              MattyUK

              Comment


              • #8
                This function will do the equivalent of toFixed() for browsers that don't support that method:

                Code:
                function formatNumber(n, p) {
                
                  var d, i, m, t;
                
                  // check arguments
                  if (isNaN(n) || isNaN(p) || p != Math.round(p) || p < 0)
                    return "Error";
                
                  // if zero decimal places was specified, just round the number
                  if (p == 0)
                    return Math.round(n).toString();
                
                  // otherwise, round to the given number of decimal places
                  m = Math.pow(10, p);
                  t = Math.round(n * m) / m;
                  t = t.toString();
                
                  // determine how many zeros need to be added after the decimal point
                  if (t.indexOf(".") < 0)
                    t += ".";
                  d = t.length - 1 - t.indexOf(".");
                
                  // add the trailing zeros
                  for (i = 0; i < p - d; i++)
                      t += "0";
                
                  return(t);
                }

                Comment


                • #9
                  Thanks all.

                  As a new member I'm really impressed with the forum.

                  Thanks again for all your help.

                  MattyUK

                  Comment


                  • #10
                    Administrator:

                    I guess this thread can be closed now. Happily got my answers.

                    Should I just leave it?

                    I'll do that and let you pick it up.

                    Cheers.

                    Comment

                    Working...
                    X