Solved

# round number in vb.net

Posted on 2010-09-16
588 Views
How do we round number in vb.net? 12.65 is a string
12.65 to 13
0
Question by:VBdotnet2005
• 7
• 3
• 2
• +1

Accepted Solution

ed_collen earned 125 total points
ID: 33697269
.NET will cast from string to decimal automatically in VB.

Dim Number As String = "12.65"
Dim DecimalNumber As Decimal = Number
Dim DecimalNumberRounded As Decimal = Math.Round(DecimalNumber,0,MidpointRounding.AwayFromZero)
0

LVL 18

Expert Comment

ID: 33698303
You can also try this Function .Call it as RoundUpDownNumber(12.65)
``````Private Function RoundUpDownNumber(ByVal val As Decimal) As Decimal

If val < Decimal.Zero Then

Return CDec(Math.Ceiling(CDbl(val) - 0.5))

Else

Return CDec(Math.Floor(CDbl(val) + 0.5))

End If

End Function
``````
0

Author Comment

ID: 33698579
I hope you don't mind if I ask this too. What if a number like this. I would like to keep ','
12,456.26  round to 12,456
112,456.45 round to 112,456
6,456.98  round to 6,457
Thank you very much in advance
0

LVL 18

Expert Comment

ID: 33698594
the function do that..but beacause it is a decimal you have to set the dec as 12456.26
0

LVL 18

Expert Comment

ID: 33698597
otherwise if you already you have the "strings" you can do that:

Dim str As String = "12,456.26"
MessageBox.Show(RoundUpToWholeNumber(Convert.ToDecimal(str.Replace(",", ""))))
0

LVL 18

Expert Comment

ID: 33698606
Sorry Wrong Function

Dim str As String = "12,456.26"
MessageBox.Show(RoundUpDownNumber(Convert.ToDecimal(str.Replace(",", ""))))
0

LVL 18

Expert Comment

ID: 33698634
Note that also @ed_collen's solution is used several times..
take a look here http://msdn.microsoft.com/en-us/library/system.math.round.aspx
0

LVL 17

Expert Comment

ID: 33701513
Just a modification of @ed_collen's code (the very first post):

Dim Number As String = "12,312.65"
Dim DecimalNumber As Decimal = Number
Dim DecimalNumberRounded As String = Math.Round(DecimalNumber, 0, MidpointRounding.AwayFromZero).ToString("###,###,###")

0

LVL 18

Expert Comment

ID: 33702810
Can i ask Why you are closing the question rather than accept a solution...there are three you may select.....
0

LVL 17

Expert Comment

ID: 33703243
You are asking the moderator to reward the points and close the question for you.
Going forward, you can do it by yourself.
0

LVL 18

Expert Comment

ID: 33703247
sorry i didn't check the close comment....very sorry....
0

Author Comment

ID: 33709022
0

Author Closing Comment

ID: 33709056
thank you
0

## Featured Post

### Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ruâ€¦
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itselâ€¦
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final pâ€¦
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Itemsâ€¦