Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to roundup a number like 16,714.29 to 17,000. But any time i use the code below it always roundup to 16,714. Can any some one help.?

What I have tried:

Try

            Dim myval As Double = TextBox17.Text

            TextBox18.Text = Math.Round(myval, 0, MidpointRounding.AwayFromZero)

        Catch ex As Exception

        End Try
Posted
Updated 6-Mar-17 4:54am
Comments
Jochen Arndt 6-Mar-17 10:08am    
Divide by thousand, round, multiply with thousand.

Try this:
Public Function RoundToLeft(d As Double, digits As Integer) As Double
	Dim rounding As Double = Math.Pow(10.0, digits)
	Return Math.Round(d / rounding, 0, MidpointRounding.AwayFromZero) * rounding
End Function

If you call it:
Console.WriteLine(RoundToLeft(d, 0))  ->  16714
Console.WriteLine(RoundToLeft(d, 1))  ->  16710
Console.WriteLine(RoundToLeft(d, 2))  ->  16700
Console.WriteLine(RoundToLeft(d, 3))  ->  17000
Console.WriteLine(RoundToLeft(d, 4))  ->  20000
 
Share this answer
 
Comments
Mathiudi 6-Mar-17 10:35am    
Thanks, but how to call this function in a button click?
OriginalGriff 6-Mar-17 10:41am    
You are kidding, right?
It's a function, like any other. It takes two parameters, and returns a double...
Here is another:
VB
Public Function Ceiling(ByVal value As Decimal, Optional ByVal nearest As Decimal = 1) As Decimal
	Return (Int(value / nearest) - If((value / nearest - Int(value / nearest) > 0D), -1D, 0D)) * nearest
End Function
To use:
VB
Dim newValue = Ceiling(3.14D, 0.5D) ' = 3.5
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900