Solved

JavaScript Math Rounding to a Decimal

Posted on 1998-09-02
8
746 Views
Last Modified: 2012-08-13
At URL below, I want to round the answer to 2 decimal places.

http://www.netafim-usa.com/calc/shrubclay.htm

Is this possible?

Thanks, April
0
Comment
Question by:aprillougheed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 4

Expert Comment

by:martinag
ID: 1270150
Here's how I do:
i *= 100;
i = Math.round(i);
i /= 100;

Maybe there's some better way? I have no idea.

Martin
0
 
LVL 8

Expert Comment

by:jbirk
ID: 1270151
martinag, why didn't you answer this one?  You provided a correct answer!
Of course those three statements can be conbined into one:
i= Math.round(i*100) / 100;

-Josh
0
 
LVL 4

Accepted Solution

by:
martinag earned 200 total points
ID: 1270152
Was that really the easiest way?
Gosh!

Martin
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 8

Expert Comment

by:jbirk
ID: 1270153
It's the only way that I have seen!
It's actually pretty simple as it is.  They probably figured it wasn't worth implementing a number of decimal places since it's so easy to get around it.
-Josh
0
 

Author Comment

by:aprillougheed
ID: 1270154
I'm sure my question was understood.  I have a formula that returns a value like
4.44444444444
and I want it to return
4.44

How does the formula accomplish this?
0
 
LVL 8

Expert Comment

by:jbirk
ID: 1270155
It translates 4.44444444444 to 444.444444444 by multiplying it by 100.  Then it rounds it normally to the nearest integer: 444, and then it divides by 100: 4.44

-josh
0
 

Author Comment

by:aprillougheed
ID: 1270156
Thanks again Josh.

I really appreciate the extra explanation.


0
 
LVL 4

Expert Comment

by:martinag
ID: 1270157
Note:
55.5555 * 100 = 5555.55

Math.round(5555.55) returns 5556
5556 / 100 = 55.56

Math.round() rounds down if the first decimal is 4 or below, up if it is five or higher.

Martin
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

705 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