MVC ASP.NET C# SQL Server WCF Written Test HR Round Subscribe C# Videos C# Programs Buy DVD

How to remove trailing zeros in a decimal - C# Program

For example, consider the sample input and expected output below.
InputOutput
1.01
1.011.01
1.00101.001
0.000
1.0050   1.005     





using System;
namespace SampleProgram
{
    class MainProgram
    {
        public static void Main(string[] args)
        {
            decimal[] decimalNumbers = { 1.0M, 1.01M, 1.0010M, 0.00M, 1.0050M};

            foreach (decimal decimalNumber in decimalNumbers)
            {
                Console.WriteLine("Original Decimal Number = {0}, Without Zeros = {1}",
                                    decimalNumber, decimalNumber.ToString("0.####"));
            }
        }
    }
}

If I enter 1.1234567890, we get 1.1235, instead of 1.123456789. This is because, the number of # symbols in the ToString("0.####") is only 4. The maximum decimal places, you want in the output is represented by the # symbol. In general a C# decimal datatype can have 29 total number of digits. This includes both integral and decimal part of a decimal.

3 comments:

  1. hai sir ,can u provide some c# programs that frequently asked some interviews regarding using STRING programs.......because it will help for all of us ............have a great pleasure for providing these blog


    thanks and regards
    yogaraj

    ReplyDelete
  2. class Program {
    static String location;
    static DateTime time;
    static void Main() { Read more
    Check this siteTeKslate for indepth C# training.
    Go here if you’re looking for information on C# Training.

    ReplyDelete