Display only 2 decimal places

Posted on 2002-03-12
Last Modified: 2006-11-17
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...

Question by:psimation
  • 2
LVL 30

Accepted Solution

third earned 50 total points
ID: 6856104

  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);
       result = whole;
    return result;

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

"javascript" math functions,

LVL 30

Expert Comment

ID: 6856106
sorry should be,

answer = 10 * 1.14 / 3.4;
answer = roundOff(answer,2);
LVL 46

Expert Comment

ID: 6856826
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
LVL 17

Author Comment

ID: 6858162
Thanks, works like a charm

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now