?
Solved

Fixing Decimal Points in Visual Basic

Posted on 2003-11-04
11
Medium Priority
?
384 Views
Last Modified: 2013-12-04
Hi,

I have some calculation in Visual Basic.  Variables are single type. I need to fix the decimal.(After decimal may be 3 digit). How can I round up ? If any body can assist me that would be great.

With thanks

Shakhawat
0
Comment
Question by:Shakhawat
8 Comments
 
LVL 3

Accepted Solution

by:
monvelasquez earned 400 total points
ID: 9684427
you can use the format function

FormattedVariable = Format(YourVariable, "0.000")
0
 
LVL 2

Expert Comment

by:lttia1
ID: 9684576
how abt this:

FormattedVariable = Round(YourVariable, 3)
0
 
LVL 1

Expert Comment

by:vkaushik
ID: 9684612
You can use the Format function to round up the expression. Following are some examples:

Dim MyStr As String

MyStr = Format(23)   ' Returns "23".

' User-defined numeric formats.
MyStr = Format(5459.4234, "##,##0.000")   ' Returns "5,459.423".
MyStr = Format(334.9723, "###0.000")   ' Returns "334.972".
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:bhagyesht
ID: 9685160
1. format function
or
2. convert to string and manuplate
0
 
LVL 1

Expert Comment

by:LastToKnow0
ID: 9690864
You can round to the nearest thousandth (instead of always down) by adding 5/10000 to the number before rounding.

Ex:
Round( 0 + 0.0005, 3) 'returns 0
Round(0.0005 + 0.0005 , 3) 'returns 0.001
Round(0.00049 + 0.0005, 3) 'returns 0

Function RoundNearest(in as Single, precision as Integer) as Single
    RoundNearest=Round(in + .5 * 10 ^(-precision), precision)
End Function
0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9699339
Shakhawat: can we have some feedback?
0
 
LVL 1

Expert Comment

by:vkaushik
ID: 9699424
Yes mate. We are awaiting the feedback.
0
 

Author Comment

by:Shakhawat
ID: 10532888
I already accepted one code and gave the point.

Shakhawat
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Starting up a Project

807 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