How to round off a decimal number in VB? - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# How to round off a decimal number in VB?

Posts: 9Member
Im developing an application where after a few calculations I get a certain value eg. 13.4
From that value I need VB to round it off to 13
If the value was 13.6 then I would want it to round it off to 14.

Hope someone has some code.

Many Thanks,

Steven

• Posts: 2,914Member
: Im developing an application where after a few calculations I get a certain value eg. 13.4
: From that value I need VB to round it off to 13
: If the value was 13.6 then I would want it to round it off to 14.
:
: Hope someone has some code.
:
Try this:-

[code]Function SRound(Real As Double) As Integer
Dim tmp As Integer
tmp = Val(Real)
If Real - tmp >= 0.5 Then
SRound = tmp + 1
Else
SRound = tmp
End If
End Function[/code]

If you want scientific rounding. Use:-

print sround(5.5) ' prints 6
print sround(4.5) ' prints 5
print sround(2.3) ' prints 2
print sround(2.6) ' prints 3

You could use Round, but then you'd get this strange behaviour:-

print round(5.5) ' prints 6
print round(4.5) ' prints [red]4[/red]
print round(2.3) ' prints 2
print round(2.6) ' prints 3

(I only bother giving this example so as to save posts from the innocent-to-VBs-many-flaws folks who think "hah, idiot re-wrote a built in" ;-))

Jonathan

###
for(74,117,115,116){\$::a.=chr};((\$_.='qwertyui')&&
(tr/yuiqwert/her anot/))for(\$::b);for(\$::c){\$_.=\$^X;
/(p.{2}l)/;\$_=\$1}\$::b=~/(..)\$/;print("\$::a\$::b \$::c hack\$1.");

• Posts: 9Member
Hi Johnathon, thanks very much for replying to my post.
I see the logic in your code but Im struggling to apply it to my application.

How would I round off a value to the next number eg. 12.1 to be rounded off to 13.

I thought it would be something like this:

Function SRound(Real As Double) As Integer

Dim tmp As Integer
tmp = Value5(Real)
If Real - tmp >= 0.0001 Then
SRound = tmp + 1
End If

End Function

Private Sub cmdCalc_Click()

value1 = txtLOR
value2 = txtLOP * comQ
value3 = txtSS

value4 = value1 - value2
value5 = value4 / value3
Call SRound
txtNOB = value5

After clicking calculate I want value5 to be rounded off to the next number but an error comes up saying argument not optional pointing to the Call SrRound line.

How would I go about structuring my code if my code is correct that is?

Would really be grateful if you could help!

Many thanks,

Steve

: : Im developing an application where after a few calculations I get a certain value eg. 13.4
: : From that value I need VB to round it off to 13
: : If the value was 13.6 then I would want it to round it off to 14.
: :
: : Hope someone has some code.
: :
: Try this:-
:
: [code]Function SRound(Real As Double) As Integer
: Dim tmp As Integer
: tmp = Val(Real)
: If Real - tmp >= 0.5 Then
: SRound = tmp + 1
: Else
: SRound = tmp
: End If
: End Function[/code]
:
: If you want scientific rounding. Use:-
:
: print sround(5.5) ' prints 6
: print sround(4.5) ' prints 5
: print sround(2.3) ' prints 2
: print sround(2.6) ' prints 3
:
: You could use Round, but then you'd get this strange behaviour:-
:
: print round(5.5) ' prints 6
: print round(4.5) ' prints [red]4[/red]
: print round(2.3) ' prints 2
: print round(2.6) ' prints 3
:
: (I only bother giving this example so as to save posts from the innocent-to-VBs-many-flaws folks who think "hah, idiot re-wrote a built in" ;-))
:
: Jonathan
:
: ###
: for(74,117,115,116){\$::a.=chr};((\$_.='qwertyui')&&
: (tr/yuiqwert/her anot/))for(\$::b);for(\$::c){\$_.=\$^X;
: /(p.{2}l)/;\$_=\$1}\$::b=~/(..)\$/;print("\$::a\$::b \$::c hack\$1.");
:
:

• Posts: 3,948Member
Umm... 13.4 you want to round down but 12.1 you want to round up?

If you want "normal" 4/5 (4 down, 5 up) rounding instead of VB's deflicticated (not sure if it's a word but I love it!) "scientific" or "mathematic" rounding (shuddup Genjuro!) then here's what I use:

[code]Public Function RealRound(ByVal Num As Double) As Long

RealRound = CLng(Num + 0.5)

End Function
[/code]

You'd use it similar to Jonathan's (which I adjusted your code below for) except that it's [b]RealRound[/b] instead of [b]SRound[/b] (and mine uses Longs instead of Integers for greater range).

Oh, and don't mess with the function, just your code, unless the function is not behaving as expected.

Finally, if you simply want to round all numbers down, use the CInt or CLng functions in VB as they do little more than strip the decimal if you provide a number to start with.

: Hi Johnathon, thanks very much for replying to my post.
: I see the logic in your code but Im struggling to apply it to my application.
:
: How would I round off a value to the next number eg. 12.1 to be rounded off to 13.
:
: I thought it would be something like this:
:
: Function SRound(Real As Double) As Integer
:
: Dim tmp As Integer
: tmp = Value5(Real)
: If Real - tmp >= 0.0001 Then
: SRound = tmp + 1
: End If
:
: End Function
:
:
: Private Sub cmdCalc_Click()
:
: value1 = txtLOR
: value2 = txtLOP * comQ
: value3 = txtSS
:
: value4 = value1 - value2
: value5 = value4 / value3
: [b]'Call SRound
: value5 = SRound(value5)[/b]
: txtNOB = value5
:
:
: After clicking calculate I want value5 to be rounded off to the next number but an error comes up saying argument not optional pointing to the Call SrRound line.
:
: How would I go about structuring my code if my code is correct that is?
:
: Would really be grateful if you could help!
:
: Many thanks,
:
: Steve
:
:
: : : Im developing an application where after a few calculations I get a certain value eg. 13.4
: : : From that value I need VB to round it off to 13
: : : If the value was 13.6 then I would want it to round it off to 14.
: : :
: : : Hope someone has some code.
: : :
: : Try this:-
: :
: : [code]Function SRound(Real As Double) As Integer
: : Dim tmp As Integer
: : tmp = Val(Real)
: : If Real - tmp >= 0.5 Then
: : SRound = tmp + 1
: : Else
: : SRound = tmp
: : End If
: : End Function[/code]
: :
: : If you want scientific rounding. Use:-
: :
: : print sround(5.5) ' prints 6
: : print sround(4.5) ' prints 5
: : print sround(2.3) ' prints 2
: : print sround(2.6) ' prints 3
: :
: : You could use Round, but then you'd get this strange behaviour:-
: :
: : print round(5.5) ' prints 6
: : print round(4.5) ' prints [red]4[/red]
: : print round(2.3) ' prints 2
: : print round(2.6) ' prints 3
: :
: : (I only bother giving this example so as to save posts from the innocent-to-VBs-many-flaws folks who think "hah, idiot re-wrote a built in" ;-))
: :
: : Jonathan
: :
: : ###
: : for(74,117,115,116){\$::a.=chr};((\$_.='qwertyui')&&
: : (tr/yuiqwert/her anot/))for(\$::b);for(\$::c){\$_.=\$^X;
: : /(p.{2}l)/;\$_=\$1}\$::b=~/(..)\$/;print("\$::a\$::b \$::c hack\$1.");
: :
: :
:
:

• Posts: 913Member
: Umm... 13.4 you want to round down but 12.1 you want to round up?
:
: If you want "normal" 4/5 (4 down, 5 up) rounding instead of VB's deflicticated (not sure if it's a word but I love it!) "scientific" or "mathematic" rounding (shuddup Genjuro!) then here's what I use:

Shuddup? Why should I?
It's nice to see someone remembers that I'm around, at least *grin*
Not that I can say much about a function composed only by a cast and a sum anyway -_-'
• Posts: 3,948Member
: Shuddup? Why should I?

Yeah, shuddup and don't point out to anyone that I put the wrong name...

: It's nice to see someone remembers that I'm around, at least *grin*

Are ya? Haven't seen you in a while.

: Not that I can say much about a function composed only by a cast and a sum anyway -_-'
:

No? You were always pretty good about finding [italic]something[/italic] wrong with my posts.