View Full Version : help in coverting numeric values
Does anybody know of a quick and easy way of converting a numeric value such as 5690967 into a formated numeric value ie
5690967 to 5,690,967
ive tried right a function that converts the numeric value to a string and then i've tried using substring, to add in the ",". I know it sounds easy but for some reason i just can't get my head around it......
CyanBlue
03-27-2003, 12:35 PM
Howdy...
I have found this from the Layer51... Try this...Number.prototype.Currency = function(uom)
{
// uom = Unit of Measure. Default set to US Dollars ($).
var temp = this.toString();
var dot = temp.indexOf(".", 0);
if (uom == null)
{
uom = "$";
}
if (dot <= 0)
{
var thedollar = temp;
var cents = "00";
}
else
{
var thedollar = temp.substr(0, dot);
var cents = temp.substr(dot + 1, temp.length);
}
if (thedollar.length > 3)
{
var d_Len = thedollar.length;
var d_brk = d_Len / 3;
if ((d_Len % 3) == 0)
{
var d_brk = (d_Len / 3) - 1;
}
for (var i = 1; i <= d_brk; i++)
{
var thedollar = thedollar.substr(0, (d_Len - (3 * i) - (i - 1))) + "," + thedollar.substr((d_Len - (3 * i) - (i - 1)), (3 * i) + (i - 1));
d_Len = thedollar.length;
}
dollars = thedollar;
}
if (cents.length > 2)
{
cents = Math.round(cents.substr(0, 2) + "." + cents.substr(2, cents.length));
}
else if (cents.length == 1)
{
cents = cents + "0";
}
return uom + dollars + "." + cents;
};
test = 12589.1550166;
trace(test.Currency());
trace(test.Currency("£ "));
senocular
03-27-2003, 12:39 PM
theres a lot of examples floating around. Heres a couple
http://proto.layer51.com/d.aspx?f=740
http://proto.layer51.com/d.aspx?f=164
the first one is what you explained, though the second allows decimal rounding and added currency sign
CyanBlue just posted the first one off the second link ;)
Billy T
03-27-2003, 01:14 PM
I just made this - could well be better ways...
cheers
Cheerz Guyz
thats great...
Ub3r
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.