Solved

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

Posted on 2014-01-09
5
386 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

12 Experts available now in Live!

Get 1:1 Help Now