round number in vb.net

How do we round number in vb.net? 12.65 is a string
12.65 to 13
VBdotnet2005Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ed_collenConnect With a Mentor Commented:
.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
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
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

Open in new window

0
 
VBdotnet2005Author Commented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
the function do that..but beacause it is a decimal you have to set the dec as 12456.26
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
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
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Sorry Wrong Function


Dim str As String = "12,456.26"
        MessageBox.Show(RoundUpDownNumber(Convert.ToDecimal(str.Replace(",", ""))))
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
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
 
ZhaolaiCommented:
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
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Can i ask Why you are closing the question rather than accept a solution...there are three you may select.....
0
 
ZhaolaiCommented:
You are asking the moderator to reward the points and close the question for you.
Going forward, you can do it by yourself.
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
sorry i didn't check the close comment....very sorry....
0
 
VBdotnet2005Author Commented:
will accept instead of requesting close, my bad
0
 
VBdotnet2005Author Commented:
thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.