Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Feedback on: irt.org FAQ Knowledge Base Q723

irt.org | About | Feedback | 4290 [ previous next ]

Feedback on:
irt.org FAQ Knowledge Base Q723

Sent by
John Westcott IV on November 25, 2002 at 10:46:39:

Worth:
Worth reading

Length:
Too long

Technical:
Too technical

Comments:
I always thought the ?: operator was rarely used. Here is something I devised after looking through code on your site. It appears to work. Let me know if you see any problems with it.

-John


function cent(amount)
{
// returns the amount in the .99 format
// First round amount to 2 places.
// If there is not a . add .00
// Otherwise
// if there is a dot with a single d followed by eol add a 0.
// if there is a dot followed by eol add a 00.
// While there are 4 d add a comma.

var commaAdder = new RegExp(/(\d*)(\d{3})(.*)/);
var needCommas = new RegExp(/\d{4}/);
var returnValue = "" + Math.round(amount * 100) / 100;

if(returnValue.search(/\./) == -1)
{returnValue = returnValue + ".00";}
else
{
returnValue = returnValue.replace(/(\.\d)$/, "$10");
returnValue = returnValue.replace(/(\.)$/, "$100");
}
while(needCommas.test(returnValue))
{returnValue = returnValue.replace(commaAdder, "$1,$2$3");}
return(returnValue);
}





Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 6th July 2009. Maintained by: Martin Webb
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2009 irt.org, All Rights Reserved.