=?Utf-8?B?T2xkIFBlZGFudA==?= wrote on 26 mei 2008 in
microsoft.public.inetserver.asp.general:
First of all, that's a fun answer!
[please always quote on usenet]
Added:
============== jscript ===========
function myFormatNumber(n,d){
var s = (n<0)?'-':'';
n = Math.floor(Math.abs(n)*Math.pow(10,d)+.5);
if (!n) s='';
if (!d) return s+n;
n += ''; while (n.length<d+1) n = '0' + n;
l = n.length;
return s+n.slice(0,l-d)+'.'+n.slice(l-d);
};
==================================
But your JS code is using Math.Floor after adding 0.5, which is okay,
but why not just use Math.Round??
Because as I said,
I want complete control over the rounding process.
And I want to be able to use a decimal point or decimal comma,
And I want to be able to set the minus behind the numberstring
or have a filler character where and if there is no minus sign,
or use a + character.
All this is possible if you do your own number to string formatting.
Ehhh...but that's a trivial
difference. Nice code. I've seen others do the same thing...in about
4 times as much code.
As I stem from prehistoric assembler coding time,
writing compact code still feels as the ultimate bliss.
What is missing is error testing for noninteger or negative values of the
number of decimals parameter.
By adding as the first line in the js function:
if (d===undefined) d = 2;
It that parameter defaults to a 2 decimal format.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)