RIAS
asked on
Limiting string to two deciamls
Hello,
How can i limit a string to 2 decimals
Cheers
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
Cheers
ASKER
Andy,
Just need to truncate it to two decimals.
Not round it but, just allow user to enter only upto two digits.
Cheers
Just need to truncate it to two decimals.
Not round it but, just allow user to enter only upto two digits.
Cheers
ASKER
Andy,
Your function is rounding it.
Your function is rounding it.
Try.. ToString("#.##")
o/p
-------
100.12
Dim a As String
a = "100.123456"
Dim r As String
r = Convert.ToDecimal(a).ToString("#.##")
MessageBox.Show(r)
o/p
-------
100.12
ASKER
Pawan,
24.99
got converted to 24
24.99
got converted to 24
ASKER
Pawan,
It is rounding it
It is rounding it
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.
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.
try using this line to replace yours.
StrMyMoney = MyMoney.ToString("N2")
Try ..Only string methods , no rounding.
O/p
100.12
Dim a As String
a = "100.129456"
Dim r As String
r = a.Substring(0, a.IndexOf(".") + 3)
MessageBox.Show(r)
O/p
100.12
ASKER
Pawan,
If input is 3000 , it converts it to 30
If input is 3000 , it converts it to 30
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)
ASKER
r = StrMoney.Substring(0, StrMoney.IndexOf(".") + 3) ---error of system argument when input is 100048.9
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cheers!
Finally :)
Cheers !
Cheers !
Any reason you didn't try my previous suggestion - one line of code is all that is required.
ASKER
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.
Your solution was had many more functions to carry on string.Moe possibility of run time errors.
Whereas,Pawans was simple functions on string.
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.
ps. It doesn't always work either.
String.Format("{0:0.00}", 123.4567) ' returns "123.46"
(or supply the formatting in the ToString function of the double)