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

    How to add Zero(0) prefix and suffix in a value.(it should be Numeric or Decimal)

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

    if TextBox value is =.1 should be insert as 00.10
    if TextBox value is =1.0 should be insert as 1.00
    if TextBox value is =1 should be insert as 1.00
    if TextBox value is =1.000 should be insert as 1.00
    if TextBox value is =0.1 should be insert as 0.10

    if TextBox value is =1234 = should be insert as 1234
    if TextBox value is =1234.22 = should be insert as 1234.22
    if TextBox value is =1234.2= should be insert as 1234.20
    if TextBox value is =1234.222 = should 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

    if i am done this task means my project is over.
    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
  • #766288
    Use Math.Round
    Math.Round(mydoublevalue, 2);

    In your code
    tbtotal2.Text = Math.Round(total2, 2).ToString();

    you can also try
    // just two decimal places
    String.Format("{0:0.##}", 123.4567); // "123.46"
    String.Format("{0:0.##}", 123.4); // "123.4"
    String.Format("{0:0.##}", 123.0); // "123"

    can also combine "0" with "#".

    String.Format("{0:0.0#}", 123.4567) // "123.46"
    String.Format("{0:0.0#}", 123.4) // "123.4"
    String.Format("{0:0.0#}", 123.0) // "123.0"

  • #766289
    There are many ways to achieve task, see below snippet

    String.Format("{0:0.00}", 140.6767554); // "140.67"
    String.Format("{0:0.00}", 140.1); // "140.10"
    String.Format("{0:0.00}", 140); // "140.00"

    Double d = 140.6767554;
    Double dc = Math.Round((Double)d, 2); // 140.67

    decimal d = 140.6767554M;
    decimal dc = Math.Round(d, 2); // 140.67

    =========
    // just two decimal places
    String.Format("{0:0.##}", 123.4567); // "123.46"
    String.Format("{0:0.##}", 123.4); // "123.4"
    String.Format("{0:0.##}", 123.0); // "123"

    can also combine "0" with "#".
    String.Format("{0:0.0#}", 123.4567) // "123.46"
    String.Format("{0:0.0#}", 123.4) // "123.4"
    String.Format("{0:0.0#}", 123.0)

    hope it helps

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

  • #766293
    Hi

    you can go through Below link

    "msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx"



    // just two decimal places
    String.Format("{0:0.00}", 123.4567); // "123.46"
    String.Format("{0:0.00}", 123.4); // "123.40"
    String.Format("{0:0.00}", 123.0); // "123.00"


    // max. two decimal places
    String.Format("{0:0.##}", 123.4567); // "123.46"
    String.Format("{0:0.##}", 123.4); // "123.4"
    String.Format("{0:0.##}", 123.0); // "123"

    // at least two digits before decimal point
    String.Format("{0:00.0}", 123.4567); // "123.5"
    String.Format("{0:00.0}", 23.4567); // "23.5"
    String.Format("{0:00.0}", 3.4567); // "03.5"
    String.Format("{0:00.0}", -3.4567); // "-03.5"

    String.Format("{0:0,0.0}", 12345.67); // "12,345.7"
    String.Format("{0:0,0}", 12345.67); // "12,346"


    String.Format("{0:0.0}", 0.0); // "0.0"
    String.Format("{0:0.#}", 0.0); // "0"
    String.Format("{0:#.0}", 0.0); // ".0"
    String.Format("{0:#.#}", 0.0); // ""

    String.Format("{0,10:0.0}", 123.4567); // " 123.5"
    String.Format("{0,-10:0.0}", 123.4567); // "123.5 "
    String.Format("{0,10:0.0}", -123.4567); // " -123.5"
    String.Format("{0,-10:0.0}", -123.4567); // "-123.5 "


    String.Format("{0:0.00;minus 0.00;zero}", 123.4567); // "123.46"
    String.Format("{0:0.00;minus 0.00;zero}", -123.4567); // "minus 123.46"
    String.Format("{0:0.00;minus 0.00;zero}", 0.0); // "zero"


    String.Format("{0:my number is 0.0}", 12.3); // "my number is 12.3"
    String.Format("{0:0aaa.bbb0}", 12.3); // "12aaa.bbb3"





    Refer Below link
    "csharp-examples.net/string-format-double/"

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766320
    Thanks for all your replies.

    i am try below code as your suggestion but i did not get exact output.

    String.Format("{0:0.00}", 123.4567); // "123.46"
    String.Format("{0:0.00}", 123.4); // "123.40"
    String.Format("{0:0.00}", 123.0); // "123.00"

    am using like below

    String.Format("{0:0.00}", txt1.Text.Trim()); // "123.46"
    String.Format("{0:0.00}", txt1.Text.Trim()); // "123.40"
    String.Format("{0:0.00}", txt1.Text.Trim()); // "123.00"

    So please suggest me to get exact output.
    thanking with
    Paul.S

  • #766325
    Below code is working these condition , that is
    if 1.0 is ==1.10
    if .1 is ==0.10
    if 111.2222 is == 111.20

    MyCode:
    string val = "1234.22";
    if (val.Split('.')[1].Length > 2)
    {
        val = val.Split('.')[0] + '.' + val.Split('.')[1].Substring(0, 1);
    }
    decimal d = Convert.ToDecimal(val);
    string result = d.ToString("0.00");
    but i want to add one more condition instead in my code that is
    If i give value = 123 it should be 123.00 that's all
    if 123 is == 123.00
    i cant able to done this. so help me to add that condition instead of my code.

    thanking with
    Paul.S

  • #766342
    Hi
    Paul
    try this code



    string num = "123";
    if (!num.Contains("."))
    {
    string a = num +".00";
    }
    else
    {
    string b = "1";
    }


    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


  • Sign In to post your comments