Display only 2 decimal places

Hi, how can I limit the output of a simple mathematical calculation to 2 decimal places.

eg. answer = 10 * 1.14 / 3.4

In he same breath, where can I find some documentation relating to java's built in math functions...

LVL 17
psimationAsked:
Who is Participating?
 
thirdCommented:

  function roundOff(value, precision){
    value = "" + value //convert value to string
    precision = parseInt(precision);

    var whole = "" + Math.round(value * Math.pow(10, precision));

    var decPoint = whole.length - precision;

    if (decPoint != 0){
       result = whole.substring(0, decPoint);
       result += ".";
       result += whole.substring(decPoint, whole.length);
    }
    else{
       result = whole;
    }
    return result;
  }


answer = 10 * 1.14 / 3.4;
answer = roundoff(answer,2);


"javascript" math functions,

http://tech.irt.org/articles/js069/

0
 
thirdCommented:
sorry should be,

answer = 10 * 1.14 / 3.4;
answer = roundOff(answer,2);
0
 
fritz_the_blankCommented:
Courtesy of Michel Plungian:

function roundAmount(n) {
  var s = "" + Math.round(n * 100) / 100
  var i = s.indexOf('.')
  if (i < 0) return s + ".00"
  var t = s.substring(0, i + 1) +
     s.substring(i + 1, i + 3)
  if (i + 2 == s.length) t += "0"
  return t
}
0
 
psimationAuthor Commented:
Thanks, works like a charm
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.