2021年1月6日星期三

c# string format 0,0 single digit has leading zero

I have a number which I want to format using thousand separator. I'm using format "0,#.##". It works nicely when I have more than single digit. However it added a leading zero when the value is single digit

using System;                        public class Program  {      public static void Main()      {          string format = "0,#.##";                    string strA = "1";          string strB = "1234";                    decimal decA = decimal.Parse(strA);          decimal decB = decimal.Parse(strB);                    string fmtA = decA.ToString(format);          string fmtB = decB.ToString(format);                    Console.WriteLine("Decimal A = " + decA); // displays 1          Console.WriteLine("Decimal B = " + decB); // displays 1234                    Console.WriteLine("Formatted A = " + fmtA); // displays 01          Console.WriteLine("Formatted B = " + fmtB); // displays 1,234      }  }  

Is there a way to make the single digit value stays a single digit?

https://stackoverflow.com/questions/65605242/c-sharp-string-format-0-0-single-digit-has-leading-zero January 07, 2021 at 08:50AM

没有评论:

发表评论