LoginSignup
1
4

More than 3 years have passed since last update.

よく忘れるstring.Format() メソッドの書式設定

Posted at

VisualStudio デコーディングしている時、よく使うけれど忘れてしまう書式設定についてメモレベルで記録しておく

数値

カンマ区切り、小数点以下固定

sample.cs
// 123,567.111
string numFormat = string.Format("{0:#,0.000}", 123456.111111);
sample.cs
// 0.000
string numFormat = string.Format("{0:#,0.000}", 0);

0の場合、「0」だけ表示

sample.cs
// 0
string numFormat = string.Format("{0:#,0.###}", 0);

0の場合、空白

sample.cs
// stringl.Empty
string numFormat = string.Format("{0:#,#.###}", 0);

先頭0埋め(コード化等に使用)

sample.cs
// 0123
string numFormat = string.Format("{0:0000}", 123);

日付

年4桁、月日2桁、24時間2桁、分秒2桁、ミリ秒表示

sample.cs
// 2021/01/01 13:01:01.123 
string dateFormat = string.Format(@"{0:yyyy/MM/dd HH:mm:ss.fff}", DateTime.Now);

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4