In some situations, for example when you want to display a number, you may want to round it to a certain number of decimal places. The Java Script Math object provides three different methods for rounding numbers. Choose the apropriate method (round, ceil, or floor ) for the situation.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site
   WARNING!
What you learn from these Totally FREE
Einstein Newsletters could cause your friends to mistake you for someone else!
  [] automobiles
  [] business
  [] parenting
  [] computers
  [] contests
  [] education
  [] entertainment
  [] food/wine
  [] free stuff
  [] genealogy
  [] health/fitness
  [] home/garden
  [] humor
  [] marketing
  [] investing
  [] pets
  [] inspiration
  [] self-improve
  [] recreation
  [] travel
  [] womens stuff
  [] writing/reading
Click here and choose as many as you like!

Round a Number

In some situations, for example when you want to display a number, you may want to round it to a certain number of decimal places. The Java Script Math object provides three different methods for rounding numbers.

Math.round

The Math.round method produces the result as we learned it in grammar school. If the decimal portion of the number is less than .5, it rounds down to the next lower integer. If the decimal portion of the number is greater than .5, it rounds up to next higher integer.

The statement Math.round(4.4) returns the value 4.
The statement Math.round(4.5) returns the value 5.

Math.floor

The Math.floor method simply drops the decimal part of the number, this causes it to always return the next integer less than or equal to the original value.

The statement Math.floor(4.4) returns the value 4.
The statement Math.floor(4.4) returns the value 4.

-  When would you want to round down? When calculating sales charges you should always round down to the nearest penny so that the benefit of rounding goes to the customer.

Math.ceil

If the Math.ceil method finds any decimal portion of a number greater zero, it returns the next integer higher than or equal to value.

The statement Math.ceil(4.4) returns the value 5.
The statement Math.ceil(4.4) returns the value 5.

-  When would you want to round up? When calculating wages paid to employees or interest earned by investors you should always round up to the nearest penny so that the benefit of rounding goes to the employee or investor.

Rounding a Number to n Decimal Places

If you were to use the Math.PI method to retreive the value pf pi, it might return a number such as 3.141592653589793. This is a combersome string of digits to deal with. You might want to round this value to 5 digits to the right of the decimal point. The line below shows how to use the Math.round method to round pi to 5 digits to the right of the decimal point.

var n = Math.round(Math.PI * 100000) / 100000;

Of course, you can round other numbers besides pi to any number of digits to the right of the decimal point. The number of zeros in the multiplier and divisor determines the number of digits to the right of the decimal point. The example below rounds the number 34.82765 to 2 digits to the right of the decimal point.

var m = 34.82765;
var n = Math.round(m * 100) / 100;

when you want to round a number a certain number of decimal places, choose the apropriate Java Script Math object method (round, ceil, or floor) for the situation.

Web Design Sections

RSS Feed RSS Feed

Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269