# 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
###### Who is Participating?

Commented:

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;

"javascript" math functions,

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

0

Commented:
sorry should be,

answer = 10 * 1.14 / 3.4;
0

Commented:
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

Author 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.