Round up Number

D

D

Hi all....
Given: total = 18.01
I use FormatNumber(total,2), it give me 18.01
I use FormatNumber(total,0) it give me 18

I wanna to get 19, how should i do the code?? if there is any decimal
value, i wanna round it up to the next whole number

cheers
 
R

Ray Costanzo [MVP]

Function RoundUp(n)
roundUp = Int(n) - CBool(CDbl(n) <> CLng(n))
End Function

That may work. What that's trying to do is take the integer portion of your
number, 18, and then, take your original decimal number, compare it to a
integerized version of it, and if they are not the same, subtract -1 (false)
from the result.

Ray at home
 
K

Ken Schaefer

Do you have to deal with negative numbers? If not then how about:

If total > FormatNumber(total,0) then
total = FormatNumber(total,0) + 1
End If

Cheers
Ken
 
E

Evertjan.

Ken Schaefer wrote on 18 okt 2004 in
microsoft.public.inetserver.asp.general:
Do you have to deal with negative numbers? If not then how about:
If total > FormatNumber(total,0) then
total = FormatNumber(total,0) + 1
End If

Why use FormatNumber() ?

====== vbscript ========

function roundup(x)
If x > Int(x) then
roundup = Int(x) + 1
Else
roundup = x
End If
End Function

total = roundup(total)

=== or in jscript ===

<script runat=server language=jscript>

function roundup(x){
return Math.ceil(x)
}

</script>

<% ' if you dedault to vbscript
total = roundup(total)
%>

======================
 
C

Coz

function roundup(x)
If x > Int(x) then
roundup = Int(x) + 1
Else
roundup = Int(x) 'note should be Int(x), not just x
End If
End Function

total = roundup(total)
 
E

Evertjan.

Coz wrote on 22 okt 2004 in microsoft.public.inetserver.asp.general:
function roundup(x)
If x > Int(x) then
roundup = Int(x) + 1
Else
roundup = Int(x) 'note should be Int(x), not just x
End If
End Function

total = roundup(total)

[please do not toppost]

"should be Int(x)":
Not quite, as Ken specified "positive numbers only".
 
E

Evertjan.

Dave wrote on 31 okt 2004 in microsoft.public.inetserver.asp.general:
The simplest way is:

FormatNumber((total + .99999999), 0)

FormatNumber(total + .99999999, 0) is simpler

both will fail miserably however on:

total=3.999999992

resulting in 5
 
D

dlbjr

Function RoundUp(num1)
RoundUp = 0
If IsNumeric(num1) Then
RoundUp = FormatNumber(CDbl(num1) + .5,0)
End If
End Function


'dlbjr
'Pleading sagacious indoctrination!
 
E

Evertjan.

dlbjr wrote on 31 okt 2004 in microsoft.public.inetserver.asp.general:
Function RoundUp(num1)
RoundUp = 0
If IsNumeric(num1) Then
RoundUp = FormatNumber(CDbl(num1) + .5,0)
End If
End Function

That is rounding, round-up is something else:

Function RoundUp(num1)
RoundUp = 0
If IsNumeric(num1) Then
RoundUp = Int(num1)
If num1 <> RoundUp Then
RoundUp = RoundUp + 1
End If
End If
End Function
 
E

Evertjan.

dlbjr wrote on 31 okt 2004 in microsoft.public.inetserver.asp.general:
What is the difference in the result?

Please quote where you are responding on.

This is not email, but usenet.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top