Solved

Limiting string to two deciamls

Posted on 2016-11-15
18
59 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 44

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
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!

 
LVL 28

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 44

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 63

Expert Comment

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

Open in new window

0
 
LVL 28

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 28

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 28

Accepted Solution

by:
Pawan Kumar earned 500 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 28

Expert Comment

by:Pawan Kumar
ID: 41889367
Finally :)

Cheers !
0
 
LVL 44

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 44

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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 …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

737 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