Solved

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

Posted on 2014-01-09
5
503 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 63

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Copying from a network share 3 38
VB6 calling COM DLL written in Visual Studio 2003 6 58
Updating Printer Details in VB.Net 16 43
Datatable / Dates ? 4 32
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 …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 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