Solved

round number in vb.net

Posted on 2010-09-16
13
606 Views
Last Modified: 2012-05-10
How do we round number in vb.net? 12.65 is a string
12.65 to 13
0
Comment
Question by:VBdotnet2005
  • 7
  • 3
  • 2
  • +1
13 Comments
 

Accepted Solution

by:
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

by:John (Yiannis) Toutountzoglou
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

Open in new window

0
 

Author Comment

by:VBdotnet2005
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
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

by:John (Yiannis) Toutountzoglou
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

by:John (Yiannis) Toutountzoglou
ID: 33698606
Sorry Wrong Function


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

Expert Comment

by:John (Yiannis) Toutountzoglou
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

by:Zhaolai
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

by:John (Yiannis) Toutountzoglou
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

by:Zhaolai
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

by:John (Yiannis) Toutountzoglou
ID: 33703247
sorry i didn't check the close comment....very sorry....
0
 

Author Comment

by:VBdotnet2005
ID: 33709022
will accept instead of requesting close, my bad
0
 

Author Closing Comment

by:VBdotnet2005
ID: 33709056
thank you
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Get Top Visible Row of Datagridview 6 33
VB.Net Report Printing Issue 3 35
SQL LINE CONTINUATION ISSUE 12 33
Store results in vb.net 3 22
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question