Solved

Is there a way to stop vb.net from rounding?

Posted on 2014-01-09
5
416 Views
Last Modified: 2014-01-11
I have a variable in a calculation with a decimal value of 0.222222222222 the format limits this to 4 decimal places so the result should be 0.2222 but the result ends up being 0.  Is there a way to make it display correctly (stop rounding)?
0
Comment
Question by:NevSoFly
5 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39769396
Can you show us some code?
0
 
LVL 40
ID: 39769618
CInt (0.222222222222 * 10000) / 10000
0
 
LVL 3

Accepted Solution

by:
13Shadow earned 250 total points
ID: 39769671
You are saying that the result variable type is decimal and you format it to only have 4 places, right?
The result variable and any other intermediate calculation variables would have to be decimal so the decimals are kept throughout the calculation.
Make sure the result variable is indeed of type decimal.

stackoverflow.com/questions/329957/truncate-decimal-number-not-round-off
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 250 total points
ID: 39769709
Good point from 13Shadow, but maybe it's not necessary

Since the result of a division is a Double, if you want to keep the value as a Decimal, then you can transform my solution to

CDec(CInt (0.222222222222 * 10000) / 10000)

However, since you want only 4 decimals, a Decimal value is overkill.
0
 

Author Closing Comment

by:NevSoFly
ID: 39769883
As It turns out one of the values was being converted into an integer as 13Shadow suggested I am going to use James' code snippet for now.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now