Solved

# format decimal

Posted on 2010-01-08
462 Views
Hi experts

Would someone show me how to convert a decimal number so that it has only 2 places to the right of the decimal point IE:

6.8474556 to 6.84
0
Question by:dizzycat

LVL 11

Expert Comment

0

LVL 30

Expert Comment

Math.Truncate(6.8474556 * 100) / 100
0

Author Comment

Still no luck
This is my code:

Dim mynumber as string = TextBox1.text
If mynumber = CDec(TextBox1.Text)Then
<<<< need code here to trim the decimal so that it has only 2 places to the right of decimal point>>>>>>>
Label1.Text = mynumber
end if

0

LVL 8

Expert Comment

myNumber.ToString("0.00")
0

Author Comment

dosent work!
0

LVL 11

Expert Comment

mynumber.tostring("#.##")
0

Author Comment

dosent work!
0

LVL 11

Expert Comment

Sorry that doesnt work because mynumber is of type string.

this should work

Dim der As String = "123.4567"
Dim derarr() As String = der.Split(".")
If derarr(1).Length > 2 Then
der = der.Remove(der.Length - 2, 2)
End If
0

LVL 27

Accepted Solution

Do it like this:

Dim mynumber As String = "6.8474556"

Dim number As String = CDec(mynumber).ToString("######.00")

MsgBox(number)

0

## Featured Post

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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 …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
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…