LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#小知識:“$”符號的作用及Format格式化字符串

admin
2024年5月21日 13:53 本文熱度 1604

作者:Hello Bug.

轉(zhuǎn)自:blog.csdn.net/LLLLL__/article/details/89505619 

   C#中$符號是從C# 6.0版本開始推出的語法糖,主要是對String.format()的簡化,當然format也不是沒有價值,本文將介紹C#中$符及String.format的一些通用用法。

一:$符號的用法

$符號的作用相當于對String.format()的簡化

例如我們需要輸出一段包含nameage的字符串:

using System;class Program{static void Main(string[] args){  string name = "liu";  int age = 10;  //夏雜麻煩的寫法  string str1 = "my name is " + name + ",my age is " + age + ",";  //使用Format的寫法  string str2 = string.Format("my name is (0),my age is (1).", name, age);//使用 $語法糖的寫法  string str3 = $"my name is {name},my age is {age}.";  //控制臺的三個輸出都相同Console.Writeline(str1)  console.Writeline(str2):  Console.Writeline(str3);  }}

——由代碼顯然可見第一種寫法復(fù)雜麻煩,使用起來是不方便的
——使用Format格式化這種寫法,需要自己在字符串中寫占位符(標記),在后面跟上參數(shù)。但是如果要格式化的參數(shù)比較多,寫起來就比較麻煩
——使用
$語法糖的寫法可讀性高,代碼簡潔。
——注意$與第一個"之前不能有空格

因為C#是美國人發(fā)明的,所以用$符號,那么如果是中國人發(fā)明的C#或許這個語法糖就使用¥符號了吧.....


二:Format格式化字符串

//將數(shù)字轉(zhuǎn)換為四舍五入后的數(shù)字,位數(shù)不足則補0Console.WriteLine(string.Format("{0:F}", 10.555));//10.56Console.WriteLine(string.Format("{0:F1}", 10.555));//10.6Console.WriteLine(string.Format("{0:F1}", 10));//10.0
//轉(zhuǎn)換為十六進制的值Console.WriteLine(string.Format("{0:X}", 10));//AConsole.WriteLine(string.Format("{0:x}", 10));//a
//轉(zhuǎn)換為當前國家的貨幣符號,默認保留2位小數(shù),會四舍五入Console.WriteLine(string.Format("{0:C}", 10.135));//¥10.14Console.WriteLine(string.Format("{0:C1}", 10.135));//¥10.1
//轉(zhuǎn)換為百分分顯示,默認保留2位小數(shù),會四舍五入Console.WriteLine(string.Format("{0:P}", 0.55));//55.00%Console.WriteLine(string.Format("{0:P0}", 0.55));//55%
//轉(zhuǎn)換成以分號分隔的數(shù)字,3位一分割,默認保留2位小數(shù)Console.WriteLine(string.Format("{0:N}", 12345));//12,345.00Console.WriteLine(string.Format("{0:N0}", 12345));//12,345
//格式化十進制的數(shù),只支持十進制的數(shù),不足則以零填充,超過精度則原數(shù)輸出Console.WriteLine(string.Format("{0:D}", 155));//155Console.WriteLine(string.Format("{0:D5}", 155));//00155
//零占位符,位數(shù)不夠則補0,會四舍五入Console.WriteLine(string.Format("{0:00.00}", 23.195));//23.20Console.WriteLine(string.Format("{0:000.00}", 23.195));//023.20
//#占位符,前后的0被舍去,會四舍五入Console.WriteLine(string.Format("{0:##.##", 23.195));//23.2Console.WriteLine(string.Format("{0:###.##}", 23.195));//23.2
//前后補空格Console.WriteLine(string.Format("{0,-5}", 99.9));//99.99 Console.WriteLine(string.Format("{0,5}", 99.9));// 99.99
//日期和時間(C#控制臺和Unity中輸出的略有區(qū)別,下面是Unity中的輸出結(jié)果)DateTime dt = new DateTime(2023, 8, 2, 13, 40, 53, 55);Console.WriteLine(string.Format("{0:D}", dt));//2023年8月2日Console.WriteLine(string.Format("{0:d}", dt));//2023/8/2Console.WriteLine(string.Format("{0:F}", dt));//2023年8月2日 13:40:53Console.WriteLine(string.Format("{0:f}", dt));//2023年8月2日 13:40Console.WriteLine(string.Format("{0:G}", dt));//2023/8/2 13:40:53Console.WriteLine(string.Format("{0:g}", dt));//2023/8/2 13:40Console.WriteLine(string.Format("{0:T}", dt));//13:40:53Console.WriteLine(string.Format("{0:t}", dt));//13:40Console.WriteLine(string.Format("{0:m}", dt));//8月2日
Console.WriteLine(string.Format("{0:yyyyMMdd}", dt));//20230802Console.WriteLine(string.Format("{0:yyyy-MM-dd}", dt));//2023-08-02Console.WriteLine(string.Format("{0:yyyy/MM/dd HH:mm:ss.fff}", dt));//2023/08/02 13:40:53.055


該文章在 2024/5/21 16:26:50 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務(wù)費用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務(wù)都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
日本天堂久久久 | 亚洲欧美在线黑人 | 亚洲a级一片在线看 | 亚洲中文字幕精品一区二区三区 | 日韩AV一区二区三区五月天 | 制服丝袜中文字幕自拍有码 |