locked
salary textbox have many 0000 for a decimal RRS feed

  • Question

  • User-944424728 posted

    <br>
    &lt;p&gt;Hello, I can input 50.0000 on my salary text box for hour and 60000.000 for salary become 60000 with no decimal. How can I make it to be 50.00 and 60000.00 or 60,000.00 on the input textbox. I had a pop up validation error but it not change in the textbox.
    The code below will work but how can I use the same textbox so the number change by itself?<br>
    thank you.&lt;/p&gt;<br>
    <br>
    Textbox2.text=Formatcurrency(textbox1.text)<br>
    Or
    Dim dec as decimal
    dec=convert.todecimal(textbox1.text)
    Textbox1.text=dec.tostring(“n2”)

    &lt;p&gt;--&lt;/p&gt;<br>
    &lt;p&gt;&lt;/p&gt;<br>
    <br>
    Thursday, June 20, 2019 4:40 PM

All replies

  • User-719153870 posted

    Hi aspvbnet,

    According to your description, you want to control the data entered into textbox to keep two decimal places. I recommend using JS and regular expressions.

    First, make sure that the input is a number, and then remove the excess decimal parts.

    Please refer to codes below:

    <script type="text/javascript">
            function num(obj) {
                obj.value = obj.value.replace(/[^\d.]/g, ""); // Clear Characters Other than Numbers and'. '
                obj.value = obj.value.replace(/^\./g, ""); // Verify that the first character is a number
                obj.value = obj.value.replace(/\.{2,}/g, "."); //Retain only the first . and remove the redundant
                obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
                obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); // Only two decimal digits can be entered
            }
        </script>
    
            <div>
                Test: 50.0000, 60000.000
                <input name="ctl00$Main_content$ucIndEmpHistory$txtSalary" type="text"
                    id="Text1" onkeyup="num(this)" onpaste="num(this)"
                    style="width: 100px; text-align: right;" wiid="C1286" runat="server" />
            </div>
    

    Hereis result of my demo:

    Best Regards,

    Yang Shen

    Friday, June 21, 2019 3:12 AM
  • User-1038772411 posted

    Hello aspvbnet,

    Try with this

    decimalVar.ToString ("0.##"); // returns "0"  when decimalVar == 0

    or Use FormatNumber:

    Dim myStr As String = "38"
    MsgBox(FormatNumber(CDbl(myStr), 2))
    //display as 38.00
    Dim myStr2 As String = "6.4" MsgBox(FormatNumber(CDbl(myStr2), 2))
    //display as 6.40

    or you can use

    Convert.ToDecimal(TextBox1.Text).ToString("0.00");

    I hope this will help you.

    you can refer this also

    https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

    Thank you

    Friday, June 21, 2019 6:48 AM