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 clsFrm As New ClsFrmMainLoad
        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

Open in new window


Cheers
RIASAsked:
Who is Participating?
 
Pawan KumarConnect With a Mentor 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)

Open in new window

0
 
AndyAinscowFreelance 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
 
RIASAuthor 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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
RIASAuthor Commented:
Andy,
Your function is rounding it.
0
 
Pawan KumarDatabase ExpertCommented:
Try.. ToString("#.##")

Dim a As String
        a = "100.123456"

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

        MessageBox.Show(r)

Open in new window


o/p
-------
100.12
0
 
RIASAuthor Commented:
Pawan,
24.99
got converted to 24
0
 
RIASAuthor Commented:
Pawan,
It is rounding it
0
 
AndyAinscowFreelance 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
 
Fernando SotoRetiredCommented:
try using this line to replace yours.
StrMyMoney = MyMoney.ToString("N2")

Open in new window

0
 
Pawan KumarDatabase 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)

Open in new window



O/p

100.12
0
 
RIASAuthor Commented:
Pawan,
If input is 3000 , it converts it to 30
0
 
Pawan KumarDatabase ExpertCommented:
Ohh..please find the updated code....

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)

Open in new window

0
 
RIASAuthor Commented:
r = StrMoney.Substring(0, StrMoney.IndexOf(".") + 3) ---error of system argument when input is 100048.9
0
 
RIASAuthor Commented:
Cheers!
0
 
Pawan KumarDatabase ExpertCommented:
Finally :)

Cheers !
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Any reason you didn't try my previous suggestion - one line of code is all that is required.
0
 
RIASAuthor 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
 
AndyAinscowFreelance 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
All Courses

From novice to tech pro — start learning today.