`
DJ阿布
  • 浏览: 30105 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

String Format for Double [C#]

阅读更多

String Format for Double [C#]

// 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"

String Format for Int [C#]

String.Format("{0:00000}", 15);          // "00015"
String.Format("{0:00000}", -15);         // "-00015"

String.Format("{0,5}", 15);              // "   15"
String.Format("{0,-5}", 15);             // "15   "
String.Format("{0,5:000}", 15);          // "  015"
String.Format("{0,-5:000}", 15);         // "015  "

String.Format("{0:#;minus #}", 15);      // "15"
String.Format("{0:#;minus #}", -15);     // "minus 15"
String.Format("{0:#;minus #;zero}", 0);  // "zero"

String.Format("{0:+### ### ### ###}", 447900123456); // "+447 900 123 456"
String.Format("{0:##-####-####}", 8958712551);       // "89-5871-2551"

String Format for DateTime [C#]

// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);

String.Format("{0:y yy yyy yyyy}", dt);  // "8 08 008 2008"   year
String.Format("{0:M MM MMM MMMM}", dt);  // "3 03 Mar March"  month
String.Format("{0:d dd ddd dddd}", dt);  // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}",     dt);  // "4 04 16 16"      hour 12/24
String.Format("{0:m mm}",          dt);  // "5 05"            minute
String.Format("{0:s ss}",          dt);  // "7 07"            second
String.Format("{0:f ff fff ffff}", dt);  // "1 12 123 1230"   sec.fraction
String.Format("{0:F FF FFF FFFF}", dt);  // "1 12 123 123"    without zeroes
String.Format("{0:t tt}",          dt);  // "P PM"            A.M. or P.M.
String.Format("{0:z zz zzz}",      dt);  // "-6 -06 -06:00"   time zone

-----------------------------------------------------------------------

 

// date separator in german culture is "." (so "/" changes to ".")
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9/3/2008 16:05:07" - english (en-US)
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9.3.2008 16:05:07" - german (de-DE)

------------------------------------------------------------------------

 

// month/day numbers without/with leading zeroes
String.Format("{0:M/d/yyyy}", dt);            // "3/9/2008"
String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"

// day/month names
String.Format("{0:ddd, MMM d, yyyy}", dt);    // "Sun, Mar 9, 2008"
String.Format("{0:dddd, MMMM d, yyyy}", dt);  // "Sunday, March 9, 2008"

// two/four digit year
String.Format("{0:MM/dd/yy}", dt);            // "03/09/08"
String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"

------------------------------------------------------------------------

 

Specifier DateTimeFormatInfo property Pattern value (for en-US culture)
t ShortTimePattern h:mm tt
d ShortDatePattern M/d/yyyy
T LongTimePattern h:mm:ss tt
D LongDatePattern dddd, MMMM dd, yyyy
f (combination of D and t) dddd, MMMM dd, yyyy h:mm tt
F FullDateTimePattern dddd, MMMM dd, yyyy h:mm:ss tt
g (combination of d and t) M/d/yyyy h:mm tt
G (combination of d and T) M/d/yyyy h:mm:ss tt
m, M MonthDayPattern MMMM dd
y, Y YearMonthPattern MMMM, yyyy
r, R RFC1123Pattern ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*)
s SortableDateTi­mePattern yyyy'-'MM'-'dd'T'HH':'mm':'ss (*)
u UniversalSorta­bleDateTimePat­tern yyyy'-'MM'-'dd HH':'mm':'ss'Z' (*)
    (*) = culture independent

------------------------------------------------------------------------

 

 String.Format("{0:t}", dt);  // "4:05 PM"                         ShortTime
String.Format("{0:d}", dt);  // "3/9/2008"                        ShortDate
String.Format("{0:T}", dt);  // "4:05:07 PM"                      LongTime
String.Format("{0:D}", dt);  // "Sunday, March 09, 2008"          LongDate
String.Format("{0:f}", dt);  // "Sunday, March 09, 2008 4:05 PM"  LongDate+ShortTime
String.Format("{0:F}", dt);  // "Sunday, March 09, 2008 4:05:07 PM" FullDateTime
String.Format("{0:g}", dt);  // "3/9/2008 4:05 PM" ShortDate+ShortTime
String.Format("{0:G}", dt);  // "3/9/2008 4:05:07 PM"             ShortDate+LongTime
String.Format("{0:m}", dt);  // "March 09"                        MonthDay
String.Format("{0:y}", dt);  // "March, 2008"                     YearMonth
String.Format("{0:r}", dt);  // "Sun, 09 Mar 2008 16:05:07 GMT"   RFC1123
String.Format("{0:s}", dt);  // "2008-03-09T16:05:07"             SortableDateTime
String.Format("{0:u}", dt);  // "2008-03-09 16:05:07Z"            UniversalSortableDateTime

分享到:
评论

相关推荐

    C# String Format Tester 1.2

    C# String Format Tester是一个用来测试字符串格式化输出的小工具。工具很简单,可以用来测试DateTime、Int16、Int32、Int64、Decimal、Single、Double、Byte、Guid 这几个类型的ToString格式化输出。此工具还集成了...

    基于c#CP3平面网严密平差数据处理

    public void GetXYZ(string PName,ref double x,ref double y,ref double z,string[] KPName, double[,] KPXYZ) { for (int i=0;i;i++) { if (PName==KPName[i]) { x=KPXYZ[i,0]; y=KPXYZ[i,1]; z=KPXYZ[i,...

    c# 动态编译代码 执行脚本代码 关键字还会变色

    Console.WriteLine(string.Format(anystr, anynuber, anyfloat)); // we can write functions string test1(int i, string s) { return s + " " + i; } double test2(double x) { double t = Math.Sin(x); // ...

    C# for CSDN 乱七八糟的看不懂

    C#关键字 关键字 abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach ...

    C#读取JPEG图片的Exif信息

    public string GetEXIFProperty(Definitions.exifCode exifCode) { // Declare local variables. string returnValue; try { // All of the EXIF properties will return strings to display in the control...

    如果在c#里面得到当前日期.pdf

    //Console.WriteLine(string.Format("{0:F4}", num)); //接收用户输入的一句英文,将其中的单词以反序输出。"hello c sharp"→"sharp c hello” string str = "hello c sharp e hello c sharp"; string[] desStr...

    GDI绘制实时频谱图形

    StringFormat format; format.SetAlignment(StringAlignmentNear); CStringW strLocalInfo; BOOL blocalMove = FALSE; PointF localPoint; ELevel* pKeepLineData = NULL; if(m_eLineType == FL_MIN_LINE) ...

    C#简易计算器

    string st = ""; double a=0, b=0; int i = 1; int flag,flag2;//初次执行算法的代号与再次执行算法的代号 bool format = false;//flag=true无需按清除,在执行下一次算法时候自动初始化 public Form1() { ...

    基于c#的图像处理源代码

    基于C#的典型图像处理算法 第二章: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; /...

    matrix.dll

    public string Outt(ref Matrix A, string format) //矩阵一维数组赋值// public void Fuzhi( ref Matrix A , double[] arr) //方阵行列式值// public double Det(ref Matrix A) //矩阵的伴随矩阵// ...

    C#实现微信红包功能

    本文实例为大家分享了C#仿微信红包功能的具体代码,供大家参考,具体内容如下 Program.cs代码: class Program { static void Main(string[] args) ... Console.WriteLine(string.Format(是否需要自定义红包金额

    根据经纬度计算图幅号

    Private Function changeToSecond(strDeg As String) As Double Dim intD As Integer, intM As Integer, dblS As Double intD = Int(strOperate(strDeg, "°").Data(0)) dblS = CDbl(Left(strOperate(strDeg, "′...

    BobBuilder_app

    Support for long string Keys with the RaptorDBString class. Duplicate keys are stored as a WAH Bitmap Index for optimal storage and speed in access. Two mode of operation Flush immediate and Deferred ...

Global site tag (gtag.js) - Google Analytics