C#小Tip:数字格式化显示
出处:http://www.jscode.cn/develop/v41101
using system; class program {
static void main() { decimal thedecnumber = 12345.678m; //the "m" creates a literal of type decimal from a double //using the tostring method //the number in the format string is the precision specifier console.writeline("no formatting: " + thedecnumber.tostring()); console.writeline("currency formatting: " + thedecnumber.tostring("c")); console.writeline("exponential formatting: " + thedecnumber.tostring("e")); console.writeline("fixed-point formatting: " + thedecnumber.tostring("f2")); console.writeline("general formatting: " + thedecnumber.tostring("g")); console.writeline("number formatting to 2 decimal places: " + thedecnumber.tostring("n2")); console.writeline("number formatting to 3 decimal places: " + thedecnumber.tostring("n3")); console.writeline("number formatting to 4 decimal places: " + thedecnumber.tostring("n4")); console.writeline("percent formatting: " + thedecnumber.tostring("p0"));
int theintnumber = 123456; console.writeline("hexidecimal formatting (for integers): {0} = {1}", theintnumber, theintnumber.tostring("x"));
double thedblnumber = 1234567890; console.writeline("custom formatting: {0} to us telephone {1}", thedblnumber, thedblnumber.tostring("(###) ### - ####"));
//keep console open if not run through command prompt console.write("\npress enter to continue"); console.readline(); } }
浙公网安备 33010602011771号