字符串的格式化

来源:互联网 发布:js调用chrome插件 编辑:程序博客网 时间:2024/06/05 02:26

 今天在CSDN上参与了一个讨论,小做总结:

http://community.csdn.net/Expert/topic/5683/5683599.xml?temp=.4862787

问题如下:

如:
decimal dec = 2.50000
如何去掉后面多作的0,显示2.5呢?
有什么方法?

///测试格式化数据
            decimal dec = 2555.50000m;
            Console.WriteLine( dec.ToString(
"0.0") );
            
string s = dec.ToString()
            
if(s.EndWith('0'))
                     s 
= s.TrimEnd('0');
            Console.WriteLine(s);

以下摘自MSDN:http://msdn2.microsoft.com/en-us/library/fht0f5be.aspx

// This code example demonstrates the String.Format() method.
// Formatting for this example uses the "en-US" culture.

using System;
class Sample 
{
    
enum Color {Yellow = 1, Blue, Green};
    
static DateTime thisDate = DateTime.Now;

    
public static void Main() 
    
{
// Store the output of the String.Format method in a string.
    string s = "";

    Console.Clear();

// Format a negative integer or floating-point number in various ways.
    Console.WriteLine("Standard Numeric Format Specifiers");
    s 
= String.Format(
        
"(C) Currency: . . . . . . . . {0:C} " +
        
"(D) Decimal:. . . . . . . . . {0:D} " +
        
"(E) Scientific: . . . . . . . {1:E} " +
        
"(F) Fixed point:. . . . . . . {1:F} " +
        
"(G) General:. . . . . . . . . {0:G} " +
        
"    (default):. . . . . . . . {0} (default = 'G') " +
        
"(N) Number: . . . . . . . . . {0:N} " +
        
"(P) Percent:. . . . . . . . . {1:P} " +
        
"(R) Round-trip: . . . . . . . {1:R} " +
        
"(X) Hexadecimal:. . . . . . . {0:X} ",
        
-123-123.45f); 
    Console.WriteLine(s);

// Format the current date in various ways.
    Console.WriteLine("Standard DateTime Format Specifiers");
    s 
= String.Format(
        
"(d) Short date: . . . . . . . {0:d} " +
        
"(D) Long date:. . . . . . . . {0:D} " +
        
"(t) Short time: . . . . . . . {0:t} " +
        
"(T) Long time:. . . . . . . . {0:T} " +
        
"(f) Full date/short time: . . {0:f} " +
        
"(F) Full date/long time:. . . {0:F} " +
        
"(g) General date/short time:. {0:g} " +
        
"(G) General date/long time: . {0:G} " +
        
"    (default):. . . . . . . . {0} (default = 'G') " +
        
"(M) Month:. . . . . . . . . . {0:M} " +
        
"(R) RFC1123:. . . . . . . . . {0:R} " +
        
"(s) Sortable: . . . . . . . . {0:s} " +
        
"(u) Universal sortable: . . . {0:u} (invariant) " +
        
"(U) Universal sortable: . . . {0:U} " +
        
"(Y) Year: . . . . . . . . . . {0:Y} "
        thisDate);
    Console.WriteLine(s);

// Format a Color enumeration value in various ways.
    Console.WriteLine("Standard Enumeration Format Specifiers");
    s 
= String.Format(
        
"(G) General:. . . . . . . . . {0:G} " +
        
"    (default):. . . . . . . . {0} (default = 'G') " +
        
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer) " +
        
"(D) Decimal number: . . . . . {0:D} " +
        
"(X) Hexadecimal:. . . . . . . {0:X} "
        Color.Green);       
    Console.WriteLine(s);
    }

}

/*
This code example produces the following results:

 

Standard Numeric Format Specifiers
(C) Currency: . . . . . . . . ($123.00)
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
    (default):. . . . . . . . -123 (default = 'G')
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85

Standard DateTime Format Specifiers
(d) Short date: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
    (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004

Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
    (default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003

原创粉丝点击