Click here to Skip to main content
15,891,691 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
i have the text box, that accepts only numbers with decimal place. But i have to find if the user has entered the more than 2 digits after decimal point, how find the length after decimal place.
Posted
Comments
creepz03 13-Dec-13 3:03am    
I would suggest you to create a regular expression. And validate it using javascript.

Since you are sure your text box contains just numbers then you might simply use the String.Split[^] method. E.g. (error checking left to the reader)

C#
string[] a = textBox1.Text.Split(new char[] { '.' });
int decimals = a[1].Length;
 
Share this answer
 
Comments
♥…ЯҠ…♥ 13-Dec-13 3:53am    
Nice and clear... 5+
CPallini 13-Dec-13 3:55am    
Thank you.
Use regulation expression to validate the entry in the textbox:

^[0-9]+([.][0-9]{2})?$


Check this out to learn to use regex in c#

http://www.dotnetperls.com/regex-match[^]
 
Share this answer
 
Comments
[no name] 13-Dec-13 4:27am    
Nice validation :)
Peter Leow 13-Dec-13 4:57am    
Thank you.
MarcusCole6833 22-Jan-16 12:21pm    
works in VB as Well
Hello ,

you can use
Decimal.TryParse Method (String, Decimal) 

to check a number is decimal or not .

Follow This Link.

thanks
 
Share this answer
 
use this


C#
string number = "4.555"; // Convert to string

int length = number.Substring(number.IndexOf(".")).Length;


or

visit here ...

http://forums.asp.net/t/1447499.aspx[^]
 
Share this answer
 
Well, you can do like this.
First : check input value is Decimal write(there many ways to determine it)
Second: input string contains fractional part of number.
Third : the length fractional part of numberof is more than 2.

See below.
C#
string[] strSplits = textbox1.Text.Split(new char[] {'.'});
            if (strSplits.Length > 2)
            {
                // write your exception code
            }
            else if (String.IsNullOrEmpty(strSplits[1]))
            {
                // write your exception code
            }
            else if (strSplits[1].Length > 2)
            {
                // write your code
            }
 
Share this answer
 
v2
Hi
Try this code.
It will allow the user to enter decimal values with 2 number after the decimal point.

noofdecimal decides how many numbers to be typed after the decimal point. you can customize according to your need.

Code Example:


XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script src="jquery-1.10.2.js" type="text/javascript"></script>
    <script type="text/javascript">

        function PressfloatOnly(evt, thisobj) {

            var charCode = (evt.which) ? evt.which : event.keyCode;
            var textboxValue = thisobj.value + "";
            if (charCode == 17 || charCode == 67)
                return true;
            if (charCode == 17 || charCode == 86)
                return true;
            if (charCode == 17 || charCode == 88)
                return true;

            if (charCode == 190 || charCode == 110) {
                var contains = textboxValue.indexOf(".") != -1;
                if (contains)
                    return false;
            }

            var temp = parseFloat(textboxValue);


            temp = temp.toFixed(2);
            var noofdecimal = 2; // it decides how many number to be printed after decimal point.
            var temp1 = textboxValue.split('.');
            if (temp1.length == 2) {
                if (temp1[1].length >= noofdecimal)// it determines that after decimal user has entered values
                    return false;
            }


            if (charCode == 37 || charCode == 39) return true;  // allow arrows
            if (charCode == 46) return true; //delete
            if (charCode == 190 || charCode == 110) return true; // period or dot
            if (charCode == 35 || charCode == 36) return true; // home, end
            if (charCode == 8 || charCode == 9) return true; // backspace , tab
            if (charCode > 47 && charCode < 58) return true; //0-9  // special character keys
            if (charCode > 95 && charCode < 106) return true; //0-9 // number keys




            return false;
        }


    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" onKeydown="return PressfloatOnly(event,this)" runat="server"></asp:TextBox>
    </form>
</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900