You must Sign In to post a response.
  • Category: ASP.NET

    How to add zero(0) if the TextBox decimal value Length more than 1

    Hi Developers,
    How to add zero(0) if the TextBox decimal value Length more than 1
    My Reqirements is as below

    if TextBox value is =1234 = sould be insert as 1234
    if TextBox value is =1234.22 = sould be insert as 1234.22
    if TextBox value is =1234.2= sould be insert as 1234.20
    if TextBox value is =1234.222 = sould be insert as 1234.20

    if the value decimal length is 1 or more than 1 na automatically add zero.
    if Decimal value less than 2 or more than 2 digits
    if 1.2 will be 1.20
    if 1.222 will be 1.20 also

    I am tried but i cant able to get exact output.so anyone know please help me to how am done this task

    thanking you
    Paul.S
  • #766195
    Hi,
    Try this:

    decimal d = Convert.ToDecimal(textbox.Text);
    string szFinalText = d.ToString("F");

  • #766202
    You can limited decimal places to 2 in many ways.
    see below snippet

    decimal d = 100.123456M;

    decimal dc = Math.Round(d, 2);
    d.ToString("#.##");
    d.ToString("F");
    OR
    public void RoundDecimalTo2dp()
    {
    decimal original = 3.14159265m;
    decimal final = Math.Round(original, 2, MidpointRounding.AwayFromZero);
    Assert.AreEqual(3.14m, final);
    }


    OR
    decimalVar.ToString("F");

    This will:

    Round off to 2 decimal places eg. 23.456 => 23.46

    Ensure that there are always 2 decimal places eg. 23 => 23.00, 12.5 => 12.50

    Ideal for currency and displaying monetary amounts

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments