Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Limiting string to two deciamls

Posted on 2016-11-15
18
Medium Priority
?
68 Views
Last Modified: 2016-11-16
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
0
Comment
Question by:RIAS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 4
  • +1
18 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41888130
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 Comment

by:RIAS
ID: 41888140
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 Comment

by:RIAS
ID: 41888141
Andy,
Your function is rounding it.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 32

Expert Comment

by:Pawan Kumar
ID: 41888152
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
 

Author Comment

by:RIAS
ID: 41888159
Pawan,
24.99
got converted to 24
0
 

Author Comment

by:RIAS
ID: 41888161
Pawan,
It is rounding it
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41888167
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41888171
try using this line to replace yours.
StrMyMoney = MyMoney.ToString("N2")

Open in new window

0
 
LVL 32

Expert Comment

by:Pawan Kumar
ID: 41888178
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
 

Author Comment

by:RIAS
ID: 41889311
Pawan,
If input is 3000 , it converts it to 30
0
 
LVL 32

Expert Comment

by:Pawan Kumar
ID: 41889321
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
 

Author Comment

by:RIAS
ID: 41889326
r = StrMoney.Substring(0, StrMoney.IndexOf(".") + 3) ---error of system argument when input is 100048.9
0
 
LVL 32

Accepted Solution

by:
Pawan Kumar earned 2000 total points
ID: 41889357
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
 

Author Closing Comment

by:RIAS
ID: 41889366
Cheers!
0
 
LVL 32

Expert Comment

by:Pawan Kumar
ID: 41889367
Finally :)

Cheers !
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41889388
Any reason you didn't try my previous suggestion - one line of code is all that is required.
0
 

Author Comment

by:RIAS
ID: 41889394
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
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41889436
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

Featured Post

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!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

604 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