Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Forcing decimal places 2

Status
Not open for further replies.

tseh

Programmer
Jan 13, 2000
64
CA
Hi.<br>
<br>
I'm writing an ecomm programme using Javascript. The problem is, the total amount shows up as 59.9 instead of 59.90. How do I force it to show two decimal places?<br>
<br>
cost += Math.floor(items<i>.price * items<i>.qty * 100)/100;<br>
write(&quot;&lt;TD&gt;&quot; + totalcost);<br>
<br>
Thanks,<br>

 
I've run into the same problem and handled it by converting the number to a string and concatenating a &quot;0&quot;. Though it works, it wasn't a satisfying approach. Anyone out there know of a direct way to resolve this problem?<br>
<br>
-- Heidi
 
Here's a good example:<br>
<A HREF=" TARGET="_new"><br>
<br>
<br>
&lt;HTML&gt;<br>
&lt;HEAD&gt;<br>
&lt;SCRIPT Language=&quot;JavaScript&quot;&gt;<br>
function roundDollar(amount)<br>
{<br>
var s = &quot;&quot;;<br>
var decimal;<br>
<br>
amount = parseFloat(amount);<br>
if (!(isNaN(amount))) {<br>
// round to nearest cent<br>
amount = Math.round(amount * 100);<br>
amount = amount / 100;<br>
<br>
// format the output<br>
s = new String(amount);<br>
decimal = s.indexOf(&quot;.&quot;);<br>
if (decimal == -1) {<br>
// whole number<br>
s+= &quot;.00&quot;;<br>
} else {<br>
if (decimal == (s.length - 2)) {<br>
// needs a trailing zero<br>
s+= &quot;0&quot;;<br>
}<br>
}<br>
} else {<br>
// not a number so return zero<br>
s = &quot;0.00&quot;;<br>
}<br>
return s;<br>
}<br>
&lt;/SCRIPT&gt;<br>
&lt;/HEAD&gt;<br>
&lt;BODY&gt;<br>
The number 5.2343 rounded to the dollar value is $&lt;SCRIPT<br>
Language=&quot;JavaScript&quot;&gt;document.write(roundDollar(5.2343));&lt;/SCRIPT&gt;<br>
&lt;/BODY&gt;<br>
&lt;/HTML&gt;
 
A Very good script ryc; but I'll edit it here to make it a little more concise for others to read ;)<br>
<br>
<b>s = new String(amount); // just creates the info string<br>
decimal = s.indexOf(&quot;.&quot;); // Finds decimal place<br>
if (decimal == -1) { //Checks if number is a whole number<br>
s+= &quot;.00&quot;; // Appends a decimal point & 2 &quot;0&quot;s to whole number<br>
} else {<br>
if (decimal == (s.length - 2)) { // Checks for single-decimal point number<br>
s+= &quot;0&quot;; //Adds a single trailing &quot;0&quot;<br>
}<br>
}</b> <p>-Robherc<br><a href=mailto:robherc@netzero.net>robherc@netzero.net</a><br><a href= > </a><br>*nix installation & program collector/reseller. Contact me if you think you've got one that I don't :)
 
Thanks Ryc;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;I just like to try to explain things a little further after I've found an answer...for 2 reasons:<br>
<br>
1: I think it really helps the recipient out a little better...this way they can learn instead of just blindly cutting & pasting ;)<br>
<br>
2: My mother gave me the middle name of &quot;Thomas&quot; (meaning: seeker of truth) so I always am looking for a deeper explanation for everything anywise ;) <p>-Robherc<br><a href=mailto:robherc@netzero.net>robherc@netzero.net</a><br><a href= > </a><br>*nix installation & program collector/reseller. Contact me if you think you've got one that I don't :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top