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

    How to add Decimal Zeros to a integer(whole) number(if value 123 is should be 123.00)

    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
  • #766335
    It is the same technique . Try the following code if you desire.

    Code:

    decimal x = 12;
    string result2 = x.ToString("0.00");
    if (x == Decimal.Parse(result2))
    {
    Console.WriteLine("Result 2 = " + result2);
    }
    Console.ReadLine();

    Output:

    Result 2 = 12.00.

  • #766336
    Thanks Mr.Tanmay Virkar. thanks a lot for your valuable reply.

    Paul.S

  • #766346
    There are multiple ways to accomplish your task
    use

    decimal sz = 23;
    string output = sz.ToString("#.##")
    //output will be 23.00
    OR
    decimal val = 23;
    twoDec = Math.Round(val, 2);
    //output will be 23.00
    //even you call string.Format it will not change original value . but it will return formatted string. so try below
    decimal szval = 23;
    string szout = string.Format("{0:0.00}", szval));

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

  • #766351
    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.

  • #766355
    Thanks for all of your valuable and quick replies dear friends.
    almost i complete the project with all of your help.

    Paul.S

  • #766893

    if(num.Equals('.')
    {

    }
    else
    {
    num = num+".00";
    }

    SRI RAMA PHANI BHUSHAN KAMBHAMPATI


  • Sign In to post your comments