Solved

# Fixing Decimal Points in Visual Basic

Posted on 2003-11-04
332 Views
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
Question by:Shakhawat

LVL 3

Accepted Solution

monvelasquez earned 100 total points
you can use the format function

FormattedVariable = Format(YourVariable, "0.000")
0

LVL 2

Expert Comment

how abt this:

FormattedVariable = Round(YourVariable, 3)
0

LVL 1

Expert Comment

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

LVL 9

Expert Comment

1. format function
or
2. convert to string and manuplate
0

LVL 1

Expert Comment

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

Shakhawat: can we have some feedback?
0

LVL 1

Expert Comment

Yes mate. We are awaiting the feedback.
0

Author Comment

I already accepted one code and gave the point.

Shakhawat
0

## Featured Post

### Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…