# Limiting string to two deciamls

Hello,
How can i limit a string to 2 decimals
``````  Public Function ConvMoney(ByVal strInfield As String) As Tuple(Of Boolean, String)
If strInfield = String.Empty Then Return Nothing
Dim MyMoney As Double
Dim StrMyMoney As String = String.Empty
Try
strInfield = strInfield.Replace(",", "")
MyMoney = Convert.ToDouble(strInfield)
StrMyMoney = MyMoney
Return New Tuple(Of Boolean, String)(True, StrMyMoney)
Catch ex As Exception
clsFrm.FormatMessage(15, " Money Information : ", strInfield, "Money information")
Return New Tuple(Of Boolean, String)(
False, String.Empty)
End Try
End Function
``````

Cheers
###### Who is Participating?

Database ExpertCommented:
Pls try..

`````` Dim a As String
a = "100048.9"
Dim p As Integer

Dim r As String

If (a.IndexOf(".") >= 0) Then
p = a.Length - a.IndexOf(".")
If p > 3 Then
p = 3
Else
p = a.Length - a.IndexOf(".")
End If

r = a.Substring(0, a.IndexOf(".")) + a.Substring(a.IndexOf("."), p)
Else
r = a
End If

MessageBox.Show(r)
``````
0

Freelance programmer / ConsultantCommented:
Use the String.Format function
String.Format("{0:0.00}", 123.4567)      ' returns "123.46"

(or supply the formatting in the ToString function of the double)
0

Author Commented:
Andy,
Just need to truncate it to two decimals.
Not round it but, just allow user to enter only upto two digits.

Cheers
0

Author Commented:
Andy,
0

Database ExpertCommented:
Try.. ToString("#.##")

``````Dim a As String
a = "100.123456"

Dim r As String
r = Convert.ToDecimal(a).ToString("#.##")

MessageBox.Show(r)
``````

o/p
-------
100.12
0

Author Commented:
Pawan,
24.99
got converted to 24
0

Author Commented:
Pawan,
It is rounding it
0

Freelance programmer / ConsultantCommented:
https://msdn.microsoft.com/en-us/library/c2eabd70(v=vs.110).aspx
Multiply by 100.0 then use Math.Truncate to truncate the double then divide by 100.0 and then format to convert to a string in that case.
0

RetiredCommented:
try using this line to replace yours.
``````StrMyMoney = MyMoney.ToString("N2")
``````
0

Database ExpertCommented:
Try ..Only string methods , no rounding.

``````Dim a As String
a = "100.129456"

Dim r As String
r = a.Substring(0, a.IndexOf(".") + 3)

MessageBox.Show(r)
``````

O/p

100.12
0

Author Commented:
Pawan,
If input is 3000 , it converts it to 30
0

Database ExpertCommented:

``````Dim a As String
a = "30000"

Dim r As String

If (a.IndexOf(".") >= 0) Then
r = a.Substring(0, a.IndexOf(".") + 3)
Else
r = a
End If

MessageBox.Show(r)
``````
0

Author Commented:
r = StrMoney.Substring(0, StrMoney.IndexOf(".") + 3) ---error of system argument when input is 100048.9
0

Author Commented:
Cheers!
0

Database ExpertCommented:
Finally :)

Cheers !
0

Freelance programmer / ConsultantCommented:
Any reason you didn't try my previous suggestion - one line of code is all that is required.
0

Author Commented:
Andy,

Your solution was had many more functions to carry on string.Moe possibility of run time errors.
Whereas,Pawans was simple functions on string.
0

Freelance programmer / ConsultantCommented:
Unless you were working with huge numbers there are no problems with run time errors.  (It is also rather simpler than the code you have chosen which is incidentally over complex)

ps.  It doesn't always work either.
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.