Solved

Limiting string to two deciamls

Posted on 2016-11-15
18
32 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
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
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 17

Expert Comment

by:Pawan Kumar Khowal
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

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

Expert Comment

by:Pawan Kumar Khowal
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 17

Accepted Solution

by:
Pawan Kumar Khowal 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 17

Expert Comment

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Open a folder with partial name 14 29
Change data in datatable 8 16
Get String split 5 33
Red error squiggly on vb.net 7 12
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

21 Experts available now in Live!

Get 1:1 Help Now