<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Javascript Round Number function</title>
	<atom:link href="http://blog.insicdesigns.com/2008/02/javascript-number-function/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.insicdesigns.com/2008/02/javascript-number-function/</link>
	<description>Web Development and Design Blog</description>
	<lastBuildDate>Wed, 11 Jan 2012 03:33:44 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
	<item>
		<title>By: ger</title>
		<link>http://blog.insicdesigns.com/2008/02/javascript-number-function/#comment-3797</link>
		<dc:creator>ger</dc:creator>
		<pubDate>Tue, 05 May 2009 08:29:41 +0000</pubDate>
		<guid isPermaLink="false">http://ninware.com/javascript-number-function#comment-3797</guid>
		<description>Actualy  there are many cases when we do not want pure math round for prices, as 
1st: prices should be always rounded up ;)  (e.g.  $4.021 =&gt; $4.05)
2nd: usually round to 5 cents (unless you have those stuppid 9.99 prices)

So, we get wierd price like $4.3126 after some calculation (item price + some delivery + WAT * % discount bla-bla-bla) and need to get nice-looking price like $4.35

function roundPrice(price) 
 {         
 var priceMultiplied = Math.ceil(price*100); 
 var reminder = priceMultiplied % 5; 
 var compliment = reminder?5-reminder:0; 
 var priceRounded = (priceMultiplied + compliment)/100; 
 return priceRounded.toFixed(2); 
 }

test cases:
roundPrice(18.32)   = 18.35
roundPrice(20.01)   = 20.05
roundPrice(21.39)   = 21.40
roundPrice(29.95)   = 29.95
roundPrice(29.96)   = 30.00
roundPrice(29.951) = 30.00</description>
		<content:encoded><![CDATA[<p>Actualy  there are many cases when we do not want pure math round for prices, as<br />
1st: prices should be always rounded up <img src='http://blog.insicdesigns.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />   (e.g.  $4.021 =&gt; $4.05)<br />
2nd: usually round to 5 cents (unless you have those stuppid 9.99 prices)</p>
<p>So, we get wierd price like $4.3126 after some calculation (item price + some delivery + WAT * % discount bla-bla-bla) and need to get nice-looking price like $4.35</p>
<p>function roundPrice(price)<br />
 {         <br />
 var priceMultiplied = Math.ceil(price*100);<br />
 var reminder = priceMultiplied % 5;<br />
 var compliment = reminder?5-reminder:0;<br />
 var priceRounded = (priceMultiplied + compliment)/100;<br />
 return priceRounded.toFixed(2);<br />
 }</p>
<p>test cases:<br />
roundPrice(18.32)   = 18.35<br />
roundPrice(20.01)   = 20.05<br />
roundPrice(21.39)   = 21.40<br />
roundPrice(29.95)   = 29.95<br />
roundPrice(29.96)   = 30.00<br />
roundPrice(29.951) = 30.00</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: insic_19</title>
		<link>http://blog.insicdesigns.com/2008/02/javascript-number-function/#comment-42</link>
		<dc:creator>insic_19</dc:creator>
		<pubDate>Wed, 13 Aug 2008 04:39:02 +0000</pubDate>
		<guid isPermaLink="false">http://ninware.com/javascript-number-function#comment-42</guid>
		<description>that is a nice idea also. thanks for the tip brian.</description>
		<content:encoded><![CDATA[<p>that is a nice idea also. thanks for the tip brian.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian McAllister</title>
		<link>http://blog.insicdesigns.com/2008/02/javascript-number-function/#comment-41</link>
		<dc:creator>Brian McAllister</dc:creator>
		<pubDate>Tue, 12 Aug 2008 14:45:48 +0000</pubDate>
		<guid isPermaLink="false">http://ninware.com/javascript-number-function#comment-41</guid>
		<description>why not use the toFixed method? It also rounds the result i.e.

var number = 3.14567
alert(number.toFixed(2));

==&gt; 3.15</description>
		<content:encoded><![CDATA[<p>why not use the toFixed method? It also rounds the result i.e.</p>
<p>var number = 3.14567<br />
alert(number.toFixed(2));</p>
<p>==&gt; 3.15</p>
]]></content:encoded>
	</item>
</channel>
</rss>

