Solved

How to round up / round down / round nearest the values?

Posted on 2009-05-13
12
1,396 Views
Last Modified: 2013-12-12
Hi,

How to round up / round down / round nearest the values? Can anyone help to start somewhere ?


If the amount is = 3333.35 then
 

Expected Out Put

-------------------------
 

Round Exact:

---------------------

Round to Exact =   $ 3333.35

Round to $1 =   $ 3333

Round to $10 =   $ 3330

Round to $100 =   $ 3300

Round to $1000 =   $ 3000
 

Round Up

-----------------------
 

Round to Exact =   $ 3333.35

Round to $1 =   $ 3334

Round to $10 =   $ 3340

Round to $100 =   $ 3400

Round to $1000 =   $ 4000
 
 

Round Down

----------------------------------
 

Round to Exact =   $ 3333.35

Round to $1 =   $ 3333

Round to $10 =   $ 3330

Round to $100 =   $ 3300

Round to $1000 =   $ 3000
 

Round nearest

---------------------------
 

Round to Exact =   $ 3333.35

Round to $1 =   $ 3333

Round to $10 =   $ 3330

Round to $100 =   $ 3300

Round to $1000 =   $ 3000

Open in new window

0
Comment
Question by:Loganathan Natarajan
  • 9
  • 3
12 Comments
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24381617
ciel (round up)
floor (round down)
round (round 4/5)
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24381657
Complete replication!
<%php

echp = '<pre>';

echo = "If the amount is = 3333.35 then\n";

$amount = 3333.35;
 

 

echo "Expected Output\n";

echo "-------------------------\n" . "\n" . "\n";

 

echo "\nRound Exact:\n";

echo "---------------------\n" . "\n";
 

echo "\nRound to Exact = \t\$" . round($amount, 2) . "\n";

echo "\nRound to $1 = \t\$" . round($amount, -1) . "\n";

echo "\nRound to $10 = \t\$" . round($amount, -2) . "\n";

echo "\nRound to $100 = \t\$" . round($amount, -3) . "\n";

echo "\nRound to $1000 = \t\$" . round($amount, -4) . "\n";

 

echo "\nRound Up\n";

echo "-----------------------\n" . "\n";

 

echo "\nRound to Exact = \t\$" . round($amount, 2) . "\n";

echo "\nRound up $1 = \t\$" . ceil($amount, -1) . "\n";

echo "\nRound up $10 = \t\$" . ceil($amount, -2) . "\n";

echo "\nRound up $100 = \t\$" . ceil($amount, -3) . "\n";

echo "\nRound up $1000 = \t\$" . ceil($amount, -4) . "\n";

 

 

echo "\nRound Down\n";

echo "-------------------------\n" . "\n";

 

echo "\nRound to Exact = \t\$" . round($amount, 2) . "\n";

echo "\nRound down $1 = \t\$" . floor($amount, -1) . "\n";

echo "\nRound down $10 = \t\$" . floor($amount, -2) . "\n";

echo "\nRound down $100 = \t\$" . floor($amount, -3) . "\n";

echo "\nRound down $1000 = \t\$" . floor($amount, -4) . "\n";

 

echo "\nRound Nearest\n";

echo "-------------------------\n" . "\n";

 

echo "\nRound to Exact = \t\$" . round($amount, 2) . "\n";

echo "\nRound to $1 = \t\$" . round($amount, -1) . "\n";

echo "\nRound to $10 = \t\$" . round($amount, -2) . "\n";

echo "\nRound to $100 = \t\$" . round($amount, -3) . "\n";

echo "\nRound to $1000 = \t\$" . round($amount, -4) . "\n";
 

%>

Open in new window

0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24381669
DOh! PHP tags.. FIXED!
<?php

echo = '<pre>';

echo = "If the amount is = 3333.35 then\n";

$amount = 3333.35;

 

 

echo "Expected Output\n";

echo "-------------------------\n" . "\n" . "\n";

 

echo "\nRound Exact:\n";

echo "---------------------\n" . "\n";

 

echo "\nRound to Exact = \t\$" . round($amount, 2) . "\n";

echo "\nRound to $1 = \t\$" . round($amount, -1) . "\n";

echo "\nRound to $10 = \t\$" . round($amount, -2) . "\n";

echo "\nRound to $100 = \t\$" . round($amount, -3) . "\n";

echo "\nRound to $1000 = \t\$" . round($amount, -4) . "\n";

 

echo "\nRound Up\n";

echo "-----------------------\n" . "\n";

 

echo "\nRound to Exact = \t\$" . round($amount, 2) . "\n";

echo "\nRound up $1 = \t\$" . ceil($amount, -1) . "\n";

echo "\nRound up $10 = \t\$" . ceil($amount, -2) . "\n";

echo "\nRound up $100 = \t\$" . ceil($amount, -3) . "\n";

echo "\nRound up $1000 = \t\$" . ceil($amount, -4) . "\n";

 

 

echo "\nRound Down\n";

echo "-------------------------\n" . "\n";

 

echo "\nRound to Exact = \t\$" . round($amount, 2) . "\n";

echo "\nRound down $1 = \t\$" . floor($amount, -1) . "\n";

echo "\nRound down $10 = \t\$" . floor($amount, -2) . "\n";

echo "\nRound down $100 = \t\$" . floor($amount, -3) . "\n";

echo "\nRound down $1000 = \t\$" . floor($amount, -4) . "\n";

 

echo "\nRound Nearest\n";

echo "-------------------------\n" . "\n";

 

echo "\nRound to Exact = \t\$" . round($amount, 2) . "\n";

echo "\nRound to $1 = \t\$" . round($amount, -1) . "\n";

echo "\nRound to $10 = \t\$" . round($amount, -2) . "\n";

echo "\nRound to $100 = \t\$" . round($amount, -3) . "\n";

echo "\nRound to $1000 = \t\$" . round($amount, -4) . "\n";

echo '</pre>';

?>

Open in new window

0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24381672
Doh! syntax fixed. Rounding precision fixed.

USE THIS CODE. NOT THE ABOVE!

<?php

echo '<pre>';

echo "If the amount is = 3333.35 then\n";

$amount = 3333.35;

 

 

echo "Expected Output\n";

echo "-------------------------\n" . "\n" . "\n";

 

echo "\nRound Exact:\n";

echo "---------------------\n" . "\n";

 

echo "\nRound to Exact = \t\$" . round($amount, 2) . "\n";

echo "\nRound to $1 = \t\$" . round($amount, 0) . "\n";

echo "\nRound to $10 = \t\$" . round($amount, -1) . "\n";

echo "\nRound to $100 = \t\$" . round($amount, -2) . "\n";

echo "\nRound to $1000 = \t\$" . round($amount, -3) . "\n";

 

echo "\nRound Up\n";

echo "-----------------------\n" . "\n";

 

echo "\nRound to Exact = \t\$" . round($amount, 2) . "\n";

echo "\nRound up $1 = \t\$" . ceil($amount, 0) . "\n";

echo "\nRound up $10 = \t\$" . ceil($amount, -1) . "\n";

echo "\nRound up $100 = \t\$" . ceil($amount, -2) . "\n";

echo "\nRound up $1000 = \t\$" . ceil($amount, -3) . "\n";

 

 

echo "\nRound Down\n";

echo "-------------------------\n" . "\n";

 

echo "\nRound to Exact = \t\$" . round($amount, 2) . "\n";

echo "\nRound down $1 = \t\$" . floor($amount, 0) . "\n";

echo "\nRound down $10 = \t\$" . floor($amount, -1) . "\n";

echo "\nRound down $100 = \t\$" . floor($amount, -2) . "\n";

echo "\nRound down $1000 = \t\$" . floor($amount, -3) . "\n";

 

echo "\nRound Nearest\n";

echo "-------------------------\n" . "\n";

 

echo "\nRound to Exact = \t\$" . round($amount, 2) . "\n";

echo "\nRound to $1 = \t\$" . round($amount, 0) . "\n";

echo "\nRound to $10 = \t\$" . round($amount, -1) . "\n";

echo "\nRound to $100 = \t\$" . round($amount, -2) . "\n";

echo "\nRound to $1000 = \t\$" . round($amount, -3) . "\n";

 

?>

Open in new window

0
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 24381673
Hello,

sorry, I want it in Javascript ...
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24381732
Oh LOL sorry I was doing it in PHP.
Here is the correct PHP code. BRB with the javascript solution!
<?php

echo '<pre>';

echo "If the amount is = 3333.35 then\n";

$amount = 3333.35;

 

 

echo "Expected Output\n";

echo "-------------------------\n" . "\n" . "\n";

 

echo "\nRound Exact:\n";

echo "---------------------\n" . "\n";

 

echo "\nRound to Exact = \t\$" . round($amount, 2) . "\n";

echo "\nRound to $1 = \t\t\$" . round($amount, 0) . "\n";

echo "\nRound to $10 = \t\t\$" . round($amount, -1) . "\n";

echo "\nRound to $100 = \t\$" . round($amount, -2) . "\n";

echo "\nRound to $1000 = \t\$" . round($amount, -3) . "\n";

 

echo "\n\n\nRound Up\n";

echo "-----------------------\n" . "\n";

 

echo "\nRound to Exact = \t\$" . round($amount, 2) . "\n";
 

// 3333.35

// 3333.3

// 3340.0

//  33.33
 
 

echo "\nRound up \$1 = \t\t\$" . ceil(round($amount, 2)) . "\n";

echo "\nRound up \$10 = \t\t\$" . ceil(round($amount)/10)*10 . "\n";

echo "\nRound up \$100 = \t\$" . ceil(round($amount)/100)*100 . "\n";

echo "\nRound up \$1000 = \t\$" . ceil(round($amount)/1000)*1000 . "\n";

 

 

echo "\n\n\nRound Down\n";

echo "-------------------------\n" . "\n";

 

echo "\nRound to Exact = \t\$" . round($amount, 2) . "\n";

echo "\nRound down \$1 = \t\$" . floor(round($amount)/1)*1 . "\n";

echo "\nRound down \$10 = \t\$" . floor(round($amount)/100)*100 . "\n";

echo "\nRound down \$100 = \t\$" . floor(round($amount)/100)*100 . "\n";

echo "\nRound down \$1000 = \t\$" . floor(round($amount)/1000)*1000 . "\n";

 

echo "\n\n\nRound Nearest\n";

echo "-------------------------\n" . "\n";

 

echo "\nRound to Exact = \t\$" . round($amount, 2) . "\n";

echo "\nRound to \$1 = \t\t\$" . round($amount, 0) . "\n";

echo "\nRound to \$10 = \t\t\$" . round($amount, -1) . "\n";

echo "\nRound to \$100 = \t\$" . round($amount, -2) . "\n";

echo "\nRound to \$1000 = \t\$" . round($amount, -3) . "\n";

 

?>

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 24381767
No, any solution using Javascript...
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24381816
Masterful!

Here is the javascript version of the REPLICATION.

=NerdsOfTech
<html>

<head>

<title>

Rounding

</title>

</head>

<body>

<pre>

<script language="Javascript">

document.write ("If the amount is = 3333.35 then\n\n");

amount = 3333.35;
 

document.write ("Expected Output\n");

document.write ("-------------------------\n" + "\n");

 

document.write ("\nRound Exact:\n");

document.write ("---------------------\n" + "\n");
 

document.write ("\nRound to Exact = \t\$" + Math.round(amount*100)/100 + "\n");

document.write ("\nRound to $1 = \t\t\$" + Math.round(amount) + "\n");

document.write ("\nRound to $10 = \t\t\$" + Math.round(amount/10)*10 + "\n");

document.write ("\nRound to $100 = \t\$" + Math.round(amount/100)*100 + "\n");

document.write ("\nRound to $1000 = \t\$" + Math.round(amount/1000)*1000 + "\n");
 
 

document.write ("\n\n\nRound Up\n");

document.write ("-----------------------\n" + "\n");

 

document.write ("\nRound to Exact = \t\$" + Math.round(amount*100)/100 + "\n");

document.write ("\nRound up \$1 = \t\t\$" + Math.ceil(amount) + "\n");

document.write ("\nRound up \$10 = \t\t\$" + Math.ceil(Math.round(amount)/10)*10 + "\n");

document.write ("\nRound up \$100 = \t\$" + Math.ceil(Math.round(amount)/100)*100 + "\n");

document.write ("\nRound up \$1000 = \t\$" + Math.ceil(Math.round(amount)/1000)*1000 + "\n");

 

 

document.write ("\n\n\nRound Down\n");

document.write ("-------------------------\n" + "\n");

 

document.write ("\nRound to Exact = \t\$" + Math.round(amount*100)/100 + "\n");

document.write ("\nRound down \$1 = \t\$" + Math.floor(amount) + "\n");

document.write ("\nRound down \$10 = \t\$" + Math.floor(Math.round(amount)/10)*10 + "\n");

document.write ("\nRound down \$100 = \t\$" + Math.floor(Math.round(amount)/100)*100 + "\n");

document.write ("\nRound down \$1000 = \t\$" + Math.floor(Math.round(amount)/1000)*1000 + "\n");

 

document.write ("\n\n\nRound Nearest\n");

document.write ("-------------------------\n" + "\n");

 

document.write ("\nRound to Exact = \t\$" + Math.round(amount*100)/100 + "\n");

document.write ("\nRound to \$1 = \t\t\$" + Math.round(amount) + "\n");

document.write ("\nRound to \$10 = \t\t\$" + Math.round(amount/10)*10 + "\n");

document.write ("\nRound to \$100 = \t\$" + Math.round(amount/100)*100 + "\n");

document.write ("\nRound to \$1000 = \t\$" + Math.round(amount/1000)*1000 + "\n");
 

</script>

<br />

</pre>
 

</body>

</html>

Open in new window

0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24381828
In your case you will need to reference

Math.round (5/4 rounding)
if tenth unit is 4 or lower round down, if tenth unit is 5 or higher round up
http://www.w3schools.com/jsref/jsref_round.asp

Math.ceil (round up)
if fraction exist round up to nearest integer
http://www.w3schools.com/jsref/jsref_ceil.asp

Math.floor (round down)
if fraction exist round down to nearest integer
http://www.w3schools.com/jsref/jsref_floor.asp

// Math.round (round)

// Math.ceil (up)

// Math.floor (down)

Open in new window

0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24381855
Also notice the pattern
document.write ("\nRound to $10 = \t\t\$" + Math.round(amount/10)*10 + "\n");
since we are rounding to the nearest 10.00
we need to divide by 10.00
round
then multiply by 10

The same pattern exist for all lines:
document.write ("\nRound to Exact = \t\$" + Math.round(amount*100)/100 + "\n");
since we are rounding to the nearest .01
we need to divide by 0.01 (or multiply by 100 in algebraical terms)
round
then multiply by 0.01 (or divide by 100 in algebraical terms)

Therefore, for complete uniform pattern sake you could have written this instead:
document.write ("\nRound to Exact = \t\$" + Math.round(amount/0.01)*0.01 + "\n");
since it is the same as
document.write ("\nRound to Exact = \t\$" + Math.round(amount*100)/100 + "\n");

Thus,
the formula is:
Math.Round(amount / precision) * precision
So if you wanted 123456789 to the nearest 100,000.
amount=123456789;

precision=100000;

document.write (Math.round(amount / precision)*precision);

//output is 123500000

Open in new window

0
 
LVL 19

Accepted Solution

by:
NerdsOfTech earned 500 total points
ID: 24381865
Here is the final solution once again. Thank you.
<html>

<head>

<title>

Rounding

</title>

</head>

<body>

<pre>

<script language="Javascript">

document.write ("If the amount is = 3333.35 then\n\n");

amount = 3333.35;
 

document.write ("Expected Output\n");

document.write ("-------------------------\n" + "\n");

 

document.write ("\nRound Exact:\n");

document.write ("---------------------\n" + "\n");
 

document.write ("\nRound to Exact = \t\$" + Math.round(amount*100)/100 + "\n");

document.write ("\nRound to $1 = \t\t\$" + Math.round(amount) + "\n");

document.write ("\nRound to $10 = \t\t\$" + Math.round(amount/10)*10 + "\n");

document.write ("\nRound to $100 = \t\$" + Math.round(amount/100)*100 + "\n");

document.write ("\nRound to $1000 = \t\$" + Math.round(amount/1000)*1000 + "\n");
 
 

document.write ("\n\n\nRound Up\n");

document.write ("-----------------------\n" + "\n");

 

document.write ("\nRound to Exact = \t\$" + Math.round(amount*100)/100 + "\n");

document.write ("\nRound up \$1 = \t\t\$" + Math.ceil(amount) + "\n");

document.write ("\nRound up \$10 = \t\t\$" + Math.ceil(Math.round(amount)/10)*10 + "\n");

document.write ("\nRound up \$100 = \t\$" + Math.ceil(Math.round(amount)/100)*100 + "\n");

document.write ("\nRound up \$1000 = \t\$" + Math.ceil(Math.round(amount)/1000)*1000 + "\n");

 

 

document.write ("\n\n\nRound Down\n");

document.write ("-------------------------\n" + "\n");

 

document.write ("\nRound to Exact = \t\$" + Math.round(amount*100)/100 + "\n");

document.write ("\nRound down \$1 = \t\$" + Math.floor(amount) + "\n");

document.write ("\nRound down \$10 = \t\$" + Math.floor(Math.round(amount)/10)*10 + "\n");

document.write ("\nRound down \$100 = \t\$" + Math.floor(Math.round(amount)/100)*100 + "\n");

document.write ("\nRound down \$1000 = \t\$" + Math.floor(Math.round(amount)/1000)*1000 + "\n");

 

document.write ("\n\n\nRound Nearest\n");

document.write ("-------------------------\n" + "\n");

 

document.write ("\nRound to Exact = \t\$" + Math.round(amount*100)/100 + "\n");

document.write ("\nRound to \$1 = \t\t\$" + Math.round(amount) + "\n");

document.write ("\nRound to \$10 = \t\t\$" + Math.round(amount/10)*10 + "\n");

document.write ("\nRound to \$100 = \t\$" + Math.round(amount/100)*100 + "\n");

document.write ("\nRound to \$1000 = \t\$" + Math.round(amount/1000)*1000 + "\n");
 

</script>

<br />

</pre>
 

</body>

</html>

Open in new window

0
 
LVL 36

Author Closing Comment

by:Loganathan Natarajan
ID: 31581307
thank you
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…

706 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

14 Experts available now in Live!

Get 1:1 Help Now