Solved

# Javascript Roun Up or Down a decimal

Posted on 2014-07-31
193 Views
How do I round up or down a decimal?  For example var myResult = 125.22545  and I wanted to round that up to two decimal points so it is 125.25?
``````<script>
var result = document.getElementById('result');
var base = parseFloat(baseOne) * parseFloat(myBox1) * parseFloat(myBox5a);
var myResult = parseFloat(base) + parseFloat(myBox2a) + parseFloat(myBox3a) + parseFloat(myBox4a);

result.innerHTML = myResult;

</script>
``````
0
Question by:rgranlund
• 2

LVL 82

Accepted Solution

leakim971 earned 250 total points
ID: 40232135
Line 6:
``````result.innerHTML = myResult.toFixed(2);
``````
0

LVL 15

Expert Comment

ID: 40232171
var myNum = Number(12.248481545454);

var myRounded = myNum.toFixed(2); // returns 12.25

result.innerHTML = myRounded;
0

LVL 7

Author Comment

ID: 40232178
Does it round up and/or down depending?
0

LVL 15

Assisted Solution

Insoftservice earned 100 total points
ID: 40232192
var original=28.453

1) //round "original" to two decimals
var result=Math.round(original*100)/100  //returns 28.45

2) // round "original" to 1 decimal
var result=Math.round(original*10)/10  //returns 28.5

3) //round 8.111111 to 3 decimals
var result=Math.round(8.111111*1000)/1000  //returns 8.111
0

## Featured Post

### Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
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)