Solved

Limiting string to two deciamls

Posted on 2016-11-15
18
53 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 62

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now