Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,566 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,895 people online right now. Registration is fast and FREE... Join Now!




how to round up or down decimal number to N decimal points

 
Reply to this topicStart new topic

how to round up or down decimal number to N decimal points

gisa
10 May, 2008 - 03:32 AM
Post #1

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 1

Hi,

as I read SQL server is using Roundup function.
In VbNet I got that Round function is working as RoundDown function.

Is there any solution in VbNet to make RoundUp function.

Thank you
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: How To Round Up Or Down Decimal Number To N Decimal Points
10 May, 2008 - 04:31 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Moved to VB.NET
User is offlineProfile CardPM
+Quote Post

jens
RE: How To Round Up Or Down Decimal Number To N Decimal Points
10 May, 2008 - 06:08 AM
Post #3

D.I.C Head
Group Icon

Joined: 9 May, 2008
Posts: 113



Thanked: 3 times
Dream Kudos: 150
My Contributions
Hello!

Generally speaking, not specifically pertaining to VB, there is a simple solution.

Suppose you have a function rounddown(double) that returns an integer rounded down - to make a function that rounds up all you have to do is:
The following is some kind of pseudo code to get the idea across (it's not in any specific language).
CODE

roundup(double) as int
  roundup = rounddown(double + 0.5)
end_roundup

Regards
Jens

This post has been edited by jens: 10 May, 2008 - 06:14 AM
User is online!Profile CardPM
+Quote Post

baavgai
RE: How To Round Up Or Down Decimal Number To N Decimal Points
10 May, 2008 - 06:42 AM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,031



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Can't be sure without an example....

Assume we have a number, 3.1415926, and you want to round down to 3.141. Round down is easy, you just have to drop the rest.

The process is to figure out significant places, multiply by a number that will move all those places before the decimal, make it an integer and divide is back, e.g. ( sorta pseudo code )
CODE

n = 3.1415926
places = 3

m = 10^places // 1000
n = n * m // 3141.5926
n = (int)n  // 3141
n = n/m  // 3.141


Rounding up, as noted, is just a 0.5 away.
CODE

n = n * m // 3141.5926
n = n + 0.5 // 3142.0926


Hope this helps.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 11:52PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month