The round function in asp doesnt round a number ending in five up. I need to
round to two digits round(num, 2) but I need it to round up when the last
digit is five, does anyone have a function for doing that ?
thank you,
Diana

Re: true rounding by Evertjan

Evertjan
Sat Aug 30 14:16:09 CDT 2003

Diana Castillo wrote on 30 aug 2003 in
microsoft.public.scripting.vbscript:
> The round function in asp doesnt round a number ending in five up. I
> need to round to two digits round(num, 2) but I need it to round up
> when the last digit is five, does anyone have a function for doing
> that ? thank you,

ASP has no round functions, vbs and jscript have !

vbs:
result = round(2.55,1)

works perfectly


jscript:
result = Math.round(2.55*10)/10

seems ok too.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Re: true rounding by Tom

Tom
Sat Aug 30 14:56:45 CDT 2003

> The round function in asp doesnt round a number ending in five up. I need
to
> round to two digits round(num, 2) but I need it to round up when the last
> digit is five, does anyone have a function for doing that ?
> thank you,
> Diana

There's also another rounding function used by statisticians: If the last
digit is 5, round to the EVEN number. Example: 5.265 would round to 5.26
but 5.275 would round to 5.28.

Tom Lake



Re: true rounding by Michael

Michael
Sat Aug 30 15:28:25 CDT 2003


"Diana Castillo" <diana.castillo@nvtechnologies.com> wrote in message
news:Od#4InybDHA.2580@TK2MSFTNGP12.phx.gbl...
:
: The round function in asp doesnt round a number ending in five up. I need to
: round to two digits round(num, 2) but I need it to round up when the last
: digit is five, does anyone have a function for doing that ?
: thank you,
: Diana


num = int((num*100)+.5)/100

10.444 = 10.44
10.445 = 10.45



Re: true rounding by Dr

Dr
Sun Aug 31 10:33:08 CDT 2003

JRS: In article <Xns93E7D85E0E168eejj99@194.109.133.29>, seen in
news:microsoft.public.scripting.vbscript, Evertjan. <exjxw.hannivoort@in
terxnl.net> posted at Sat, 30 Aug 2003 19:16:09 :-
>Diana Castillo wrote on 30 aug 2003 in
>microsoft.public.scripting.vbscript:
>> The round function in asp doesnt round a number ending in five up. I
>> need to round to two digits round(num, 2) but I need it to round up
>> when the last digit is five, does anyone have a function for doing
>> that ? thank you,
>
>ASP has no round functions, vbs and jscript have !
>
>vbs:
>result = round(2.55,1)
>
>works perfectly
>
>
>jscript:
>result = Math.round(2.55*10)/10
>
>seems ok too.

Since 2.55 is presumably stored in floating-point binary, it cannot be
stored exactly, and so it might need to be rounded either way. Tests
should be done on multiples of power-of-two fractions, such as 05 0.25
0.75 0.125 0.376 0.0625 etc. The quaint US habit of measuring in such
fractions of something that they call an inch should help them here.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

Re: true rounding by Evertjan

Evertjan
Sun Aug 31 13:33:29 CDT 2003

Dr John Stockton wrote on 31 aug 2003 in
microsoft.public.scripting.vbscript:
> Since 2.55 is presumably stored in floating-point binary, it cannot be
> stored exactly, and so it might need to be rounded either way.
> Tests
> should be done on multiples of power-of-two fractions, such as 05 0.25
> 0.75 0.125 0.376 0.0625 etc.

Then an absolute good rounding is impossible with interpreters with
binary floating point engines, I think, however thoroughly you test.

It is possible to make an exact round() function that is totally
independent of the binary floating point engine's quirks:


<script type="text/javascript">

alert(round("12345.55",1))
alert(round("12345.22224",4))
alert(round("12345.22227",4))
alert(round("12345.55",8))
alert(round("12345.55",0))
alert(round("12345",4))

function round(n,d) {
var a = n.split(".")
if((d<1)||(a.length<2))return a[0]
r = a[1].substr(0,d)
v = a[1].substr(d,1)
r = (v<5)?1*r:1*r+1

return a[0]+"."+r
}
</script>

> The quaint US habit of measuring in such
> fractions of something that they call an inch should help them here.

So true.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Re: true rounding by Evertjan

Evertjan
Sun Aug 31 13:55:41 CDT 2003

Evertjan. wrote on 31 aug 2003 in microsoft.public.scripting.vbscript:

> <script type="text/javascript">
>
> alert(round("12345.55",1))
> alert(round("12345.22224",4))
> alert(round("12345.22227",4))
> alert(round("12345.55",8))
> alert(round("12345.55",0))
> alert(round("12345",4))
>
> function round(n,d) {
> var a = n.split(".")
> if((d<1)||(a.length<2))return a[0]
> r = a[1].substr(0,d)
> v = a[1].substr(d,1)
> r = (v<5)?1*r:1*r+1
>
> return a[0]+"."+r
>}
> </script>
>

Far from perfect, try this please:

<script type="text/javascript">
alert(round("-12345.55",1))
alert(round("12345.00224",4))
alert(round("12345.00227",4))
alert(round("12345.55",8))
alert(round("12345.55",0))
alert(round("12345",4))

function round(n,d) {

var a = n.split(".")
if((d<1)||(a.length<2))return a[0]
a[0] = Math.abs(a[0])
r = a[0]+"."+a[1].substr(0,d)
v = a[1].substr(d,1)
if(v>=5) r=1*r+Math.pow(10,-d)
if(n<0) r=-r
return r
}
</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Re: true rounding by Dr

Dr
Mon Sep 01 11:33:14 CDT 2003

JRS: In article <Od#4InybDHA.2580@TK2MSFTNGP12.phx.gbl>, seen in
news:microsoft.public.scripting.vbscript, Diana Castillo <diana.castillo
@nvtechnologies.com> posted at Sat, 30 Aug 2003 21:04:11 :-
>
>The round function in asp doesnt round a number ending in five up. I need to
>round to two digits round(num, 2) but I need it to round up when the last
>digit is five, does anyone have a function for doing that ?

This is a VBScript newsgroup.

I've only seen the combination of asp & javascript before.

One wonders exactly what you mean by "a number ending in five". A
number may be represented as a string, or as a binary integer, or in
floating-point, usually binary.

Only when in string form can it really be said to end in 5.

If you need to round such as 12.345, then it cannot be an integer. It
can be a string, in which case it is best to detect the 5 using string
operations. If it is a number, then it cannot (in likely circumstances)
be exact, since 0.345 is not a multiple of 2^-n for n <= likely mantissa
length. Therefore, a proper mathematical rounding may either round it
up or down.

See <URL:http://www.merlyn.demon.co.uk/js-round.htm> for rounding in
javascript; see also <URL:http://www.merlyn.demon.co.uk/pas-chop.htm>.

Beware of javascript "solutions" that work only with specific browsers.

Note that rounding to two digits implies string output, and that 0.00,
1.00, 1.30 are possible results. It is not the same as rounding to a
multiple of 0.01 or of 1/100 with a result of numeric type.

Variations of the following give interesting results with Evertjan's
second function in my browser.

for (J=12290 ; J<=12410 ; J+=5) {
Q = J*0.001 // or J/1000
document.write(J, ' ', Q, ' ', round(Q, 2), '<br>')
}

Note that J*0.001 is not always equal to J/1000.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.