divisiorn problem

E

Evertjan.

..nLL wrote on 16 mei 2008 in microsoft.public.inetserver.asp.general:
<%=2/33334%>

returns 5.99988000239995E-05

how can i convert it to a n.nn ?

Use:

<% = "n.nn" %>

;-)


Better lookup vbs: FormatNumber()
 
O

Old Pedant

Neil Gould said:
VBScript:

<%=Round(2/3334, 2)%>

No, that will actually display simply
0

That's because his number, rounded to 2 decimal places, is 0.00 (no non-zero
digits). But *AS A NUMBER*, there is no difference between 0.00 and just
plain 0.

So when you Response.Write zero, you get simply
0
Nothing more.

You *MUST* use
FormatNumber( 2/33334, 2 )
or equivalent if you really want to see
0.00
displayed.
 
E

Evertjan.

=?Utf-8?B?T2xkIFBlZGFudA==?= wrote on 26 mei 2008 in
microsoft.public.inetserver.asp.general:
No, that will actually display simply
0

That's because his number, rounded to 2 decimal places, is 0.00 (no
non-zero digits). But *AS A NUMBER*, there is no difference between
0.00 and just plain 0.

So when you Response.Write zero, you get simply
0
Nothing more.

You *MUST* use
FormatNumber( 2/33334, 2 )
or equivalent if you really want to see
0.00
displayed.

You could bake your own,
giving you power over the rounding process:

========= vbs ==============
function myFormatNumber(n,d)
if n<0 then s = "-" else s = ""
n = fix(abs(n)*10^d + 0.5)
if n=0 then s=""
while len(n)<d+1
n = "0"&n
wend
myFormatNumber = s&left(n,len(n)-d)&"."&right(n,d)
if d=0 then myFormatNumber = s&n
end function
===========================
 
E

Evertjan.

Evertjan. wrote on 26 mei 2008 in microsoft.public.inetserver.asp.general:
You could bake your own,
giving you power over the rounding process:

========= vbs ==============
function myFormatNumber(n,d)
if n<0 then s = "-" else s = ""
n = fix(abs(n)*10^d + 0.5)
if n=0 then s=""
while len(n)<d+1
n = "0"&n
wend
myFormatNumber = s&left(n,len(n)-d)&"."&right(n,d)
if d=0 then myFormatNumber = s&n
end function
===========================

============== 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);
};
==================================
 
O

Old Pedant

First of all, that's a fun answer!

But your JS code is using Math.Floor after adding 0.5, which is okay, but
why not just use Math.Round?? Ehhh...but that's a trivial difference.
Nice code. I've seen others do the same thing...in about 4 times as much
code.
 
E

Evertjan.

=?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 &nbsp; 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,206
Latest member
Zenden

Latest Threads

Top