Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 792
  • Last Modified:

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

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
NevSoFly
Asked:
NevSoFly
2 Solutions
 
Fernando SotoRetiredCommented:
Can you show us some code?
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
CInt (0.222222222222 * 10000) / 10000
0
 
13ShadowCommented:
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
 
Jacques Bourgeois (James Burger)PresidentCommented:
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
 
NevSoFlyAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now