注æ
ãã®ãã¼ã¸ã«ã¢ã¯ã»ã¹ããã«ã¯ãæ¿èªãå¿ è¦ã§ãã ãµã¤ã³ã¤ã³ã¾ãã¯ãã£ã¬ã¯ããªã®å¤æ´ã試ããã¨ãã§ãã¾ãã
ãã®ãã¼ã¸ã«ã¢ã¯ã»ã¹ããã«ã¯ãæ¿èªãå¿ è¦ã§ãã ãã£ã¬ã¯ããªã®å¤æ´ã試ããã¨ãã§ãã¾ãã
æ¸å¼è¨å®ã¯ãã¯ã©ã¹ã¾ãã¯æ§é ä½ã®ã¤ã³ã¹ã¿ã³ã¹ãã¾ãã¯åæå¤ããæåå表ç¾ã«å¤æããããã»ã¹ã§ãã ãã®ç®çã¯ãçµæã®æååãã¦ã¼ã¶ã¼ã«è¡¨ç¤ºããããå¾ã§å ã®ãã¼ã¿åã復å ããããã«éã·ãªã¢ã«åããããããã¨ã§ãã ãã®è¨äºã§ã¯ã.NET ã§æä¾ãããæ¸å¼è¨å®ã®ã¡ã«ããºã ã«ã¤ãã¦èª¬æãã¾ãã
注
è§£æã¯æ¸å¼è¨å®ã®éã®æä½ã§ã ãã¼ã¹æä½ã¯ãã®æåå表ç¾ãããã¼ã¿åã®ã¤ã³ã¹ã¿ã³ã¹ã使ãã¾ãã 詳ããã¯ãæååã®è§£æã«é¢ããè¨äºãã覧ãã ããã ã·ãªã¢ã«åã¨éã·ãªã¢ã«åã«ã¤ãã¦ã¯ãã.NET ã§ã®ã·ãªã¢ã«åããã覧ãã ããã
åºæ¬çãªæ¸å¼è¨å®ã®æ¹å¼ã¯ãObject.ToString ã¡ã½ããã«ãã£ã¦æ¢å®ã¨ãã¦å®è£ ããã¾ãããã®ã¡ã½ããã«ã¤ãã¦ã¯ããã®ãããã¯ã®ãToString ã¡ã½ããã使ç¨ããæ¢å®ã®æ¸å¼è¨å® ãã®ã»ã¯ã·ã§ã³ãåç §ãã¦ãã ããã ãã ãã.NET ã«ã¯ããã®æ¢å®ã®æ¸å¼è¨å®æ©è½ã夿´ããã³æ¡å¼µããæ¹æ³ãããã¤ãããã¾ãã æ¬¡ã«ä¾ã示ãã¾ãã
Object.ToString ã¡ã½ããããªã¼ãã¼ã©ã¤ããã¦ããªãã¸ã§ã¯ãã®å¤ã®ã«ã¹ã¿ã æååå½¢å¼ãå®ç¾©ããã 詳細ã«ã¤ãã¦ã¯ããã®ãããã¯ã®å¾ã®ãToString ã¡ã½ããã®ãªã¼ãã¼ã©ã¤ããã»ã¯ã·ã§ã³ãåç §ãã¦ãã ããã
ãªãã¸ã§ã¯ãã®å¤ã®æååå½¢å¼ã«è¤æ°ã®å½¢å¼ãæãããæ¸å¼æå®åãå®ç¾©ããã ãã¨ãã°ã次ã®ã¹ãã¼ãã¡ã³ãã§ã¯ "X" æ¸å¼æå®åã使ç¨ãã¦æ´æ°ã 16 é²å¤ã®æååå½¢å¼ã«å¤æãã¾ãã
int integerValue = 60312; Console.WriteLine(integerValue.ToString("X")); // Displays EB98.
Dim integerValue As Integer = 60312 Console.WriteLine(integerValue.ToString("X")) ' Displays EB98.
æ¸å¼æå®åã®è©³ç´°ã«ã¤ãã¦ã¯ãã ToString ã¡ã½ããã¨æ¸å¼æå®æåå ãã®ã»ã¯ã·ã§ã³ãåç §ãã¦ãã ããã
æ¸å¼ãããã¤ãã¼ã使ç¨ãã¦ãç¹å®ã®ã«ã«ãã£ã®æ¸å¼æå®è¦åãå®è£ ããã ãã¨ãã°ã次ã®ã¹ãã¼ãã¡ã³ãã§ã¯ en-US ã«ã«ãã£ã®æ¸å¼æå®è¦åã使ç¨ãã¦é貨å¤ã表示ãã¾ãã
double cost = 1632.54; Console.WriteLine(cost.ToString("C", new System.Globalization.CultureInfo("en-US"))); // The example displays the following output: // $1,632.54
Dim cost As Double = 1632.54 Console.WriteLine(cost.ToString("C", New System.Globalization.CultureInfo("en-US"))) ' The example displays the following output: ' $1,632.54
æ¸å¼ãããã¤ãã¼ã使ç¨ããæ¸å¼è¨å®ã®è©³ç´°ã«ã¤ãã¦ã¯ãæ¸å¼ãããã¤ãã¼ã«é¢ããã»ã¯ã·ã§ã³ãåç §ãã¦ãã ããã
IFormattable ã¤ã³ã¿ã¼ãã§ã¤ã¹ãå®è£ ãã¦ã Convert ã¯ã©ã¹ã«ããæåå夿ã¨è¤åæ¸å¼æå®ã®ä¸¡æ¹ããµãã¼ãããæ¹æ³ã 詳細ã«ã¤ãã¦ã¯ãã IFormattable ã¤ã³ã¿ã¼ãã§ã¤ã¹ ãã®ã»ã¯ã·ã§ã³ãåç §ãã¦ãã ããã
è¤åæ¸å¼æå®ã使ç¨ãã¦ãå¤ã®æååå½¢å¼ã大ããªæååã«åãè¾¼ãæ¹æ³ã 詳細ã«ã¤ãã¦ã¯ãã è¤åæ¸å¼æå® ãã®ã»ã¯ã·ã§ã³ãåç §ãã¦ãã ããã
å¤ã®æåå表ç¾ã大ããªæååã«åãè¾¼ãããã®ããã«èªã¿ãããæ§æã§ããæååè£éã使ç¨ããã 詳細ã«ã¤ãã¦ã¯ãæååè£éã«é¢ãããããã¯ãåç §ãã¦ãã ããã
ICustomFormatter ããã³ IFormatProvider ãå®è£ ãã¦ãå®å ¨ãªã«ã¹ã¿ã æ¸å¼è¨å®ã½ãªã¥ã¼ã·ã§ã³ãæä¾ããæ¹æ³ã 詳細ã«ã¤ãã¦ã¯ãã ICustomFormatter ã使ç¨ããã«ã¹ã¿ã æ¸å¼è¨å® ãã®ã»ã¯ã·ã§ã³ãåç §ãã¦ãã ããã
以éã®ã»ã¯ã·ã§ã³ã§ã¯ããªãã¸ã§ã¯ããæååå½¢å¼ã«å¤æãããããã®æ¹æ³ã«ã¤ãã¦èª¬æãã¾ãã
ToString ã¡ã½ããã使ç¨ããæ¢å®ã®æ¸å¼è¨å®
System.Object ããæ´¾çãããã¹ã¦ã®åã¯ãæ¢å®ã§åã®ååãè¿ãããã©ã¡ã¼ã¿ã¼ãªãã® ToString
ã¡ã½ãããèªåçã«ç¶æ¿ãã¾ãã æ¢å®ã® ToString
ã¡ã½ããã®ä¾ã次ã«ç¤ºãã¾ãã ãã®ã³ã¼ãä¾ã§ã¯ãå®è£
ãæããªã Automobile
ã¨ããååã®ã¯ã©ã¹ãå®ç¾©ãã¾ãã ãã®ã¯ã©ã¹ãã¤ã³ã¹ã¿ã³ã¹åããã ToString
ã¡ã½ãããå¼ã³åºãããã¨ããã®åã®ååã表示ããã¾ãã ãµã³ãã«ã§ã¯ã ToString
ã¡ã½ãããæç¤ºçã«å¼ã³åºãããªããã¨ã«æ³¨æãã¦ãã ããã Console.WriteLine(Object) ã¡ã½ããã¯å¼æ°ã¨ãã¦æ¸¡ããããªãã¸ã§ã¯ãã® ToString
ã¡ã½ãããæé»çã«å¼ã³åºãã¾ãã
using System;
public class Automobile
{
// No implementation. All members are inherited from Object.
}
public class Example9
{
public static void Main()
{
Automobile firstAuto = new Automobile();
Console.WriteLine(firstAuto);
}
}
// The example displays the following output:
// Automobile
Public Class Automobile
' No implementation. All members are inherited from Object.
End Class
Module Example9
Public Sub Main9()
Dim firstAuto As New Automobile()
Console.WriteLine(firstAuto)
End Sub
End Module
' The example displays the following output:
' Automobile
è¦å
Windows 8 以éãWindows ã©ã³ã¿ã¤ã ã«ã¯ãæ¢å®ã®æ¸å¼æå®ããµãã¼ãããåä¸ã®ã¡ã½ãã IStringable ãåãã ã¤ã³ã¿ã¼ãã§ã¤ã¹ãå«ã¾ãã¦ãã¾ãã ãã ããããã¼ã¸ãåã§ã¯ IStringable
ã¤ã³ã¿ã¼ãã§ã¤ã¹ãå®è£
ããªããã¨ããå§ããã¾ãã 詳細ã«ã¤ãã¦ã¯ããWindows ã©ã³ã¿ã¤ã 㨠IStringable ã¤ã³ã¿ã¼ãã§ã¤ã¹ãåç
§ãã¦ãã ããã
ã¤ã³ã¿ã¼ãã§ã¤ã¹ä»¥å¤ã®åã¯ãã¹ã¦ Objectããæ´¾çããããããã®æ©è½ã¯ã«ã¹ã¿ã ã®ã¯ã©ã¹ã¾ãã¯æ§é ä½ã«èªåçã«æä¾ããã¾ãã ãã ããæ¢å®ã® ToString
ã¡ã½ããã§æä¾ãããæ©è½ã«ã¯å¶éãããã¾ããåãèå¥ãã¾ãããåã®ã¤ã³ã¹ã¿ã³ã¹ã«é¢ããæ
å ±ãæå®ããã¨ãã¨ã©ã¼ã«ãªãã¾ãã ããèªä½ã«é¢ããæ
å ±ãæä¾ãããªãã¸ã§ã¯ãã®æååå½¢å¼ãæä¾ããã«ã¯ã ToString
ã¡ã½ããããªã¼ãã¼ã©ã¤ãããå¿
è¦ãããã¾ãã
注
æ§é ä½ã¯ã ValueTypeããæ´¾çãã Objectãç¶æ¿ãã¾ãã ValueType 㯠Object.ToStringããªã¼ãã¼ã©ã¤ããã¾ããããã®å®è£ ã¯åãã§ãã
ToString ã¡ã½ããã®ãªã¼ãã¼ã©ã¤ã
ååã表示ãããã¨ã¯æç¨æ§ãéããã¦ãããåã®å©ç¨è
ãã¤ã³ã¹ã¿ã³ã¹ãä»ã®ã¤ã³ã¹ã¿ã³ã¹ããåºå¥ãããã¨ã¯ã§ãã¾ããã ããããToString
ã¡ã½ããããªã¼ãã¼ã©ã¤ããã¦ããã£ã¨å½¹ã«ç«ã¤ãªãã¸ã§ã¯ãã®å¤ã®å½¢å¼ã使ãããã¨ãã§ãã¾ãã æ¬¡ã®ä¾ã§ã¯ Temperature
ãªãã¸ã§ã¯ããå®ç¾©ãããã® ToString
ã¡ã½ããããªã¼ãã¼ã©ã¤ããã¦ãæ¸©åº¦ãææ°ã§è¡¨ç¤ºãã¾ãã
public class Temperature
{
private decimal temp;
public Temperature(decimal temperature)
{
this.temp = temperature;
}
public override string ToString()
{
return this.temp.ToString("N1") + "°C";
}
}
public class Example12
{
public static void Main()
{
Temperature currentTemperature = new Temperature(23.6m);
Console.WriteLine($"The current temperature is {currentTemperature}");
}
}
// The example displays the following output:
// The current temperature is 23.6°C.
Public Class Temperature
Private temp As Decimal
Public Sub New(temperature As Decimal)
Me.temp = temperature
End Sub
Public Overrides Function ToString() As String
Return Me.temp.ToString("N1") + "°C"
End Function
End Class
Module Example13
Public Sub Main13()
Dim currentTemperature As New Temperature(23.6D)
Console.WriteLine("The current temperature is " +
currentTemperature.ToString())
End Sub
End Module
' The example displays the following output:
' The current temperature is 23.6°C.
.NET ã§ã¯ãåããªããã£ãå¤åã® ToString
ã¡ã½ããã¯ãååã®ä»£ããã«ãªãã¸ã§ã¯ãã®å¤ã表示ããããã«ãªã¼ãã¼ã©ã¤ãããã¦ãã¾ãã åããªããã£ãåã®ãªã¼ãã¼ã©ã¤ããæ¬¡ã®è¡¨ã«ç¤ºãã¾ãã ãªã¼ãã¼ã©ã¤ãããã¦ããã¡ã½ããã®ã»ã¨ãã©ã¯ ToString
ã¡ã½ããã®å¥ã®ãªã¼ãã¼ãã¼ããå¼ã³åºããããã«ãã®åã®ä¸è¬æ¸å¼ãå®ç¾©ãã "G" æ¸å¼æå®åã¨ãç¾å¨ã®ã«ã«ãã£ã表ã IFormatProvider ãªãã¸ã§ã¯ããæ¸¡ãã¾ãã
ã¿ã¤ã | ToString ã®ãªã¼ãã¼ã©ã¤ã |
---|---|
Boolean | Boolean.TrueString ã¾ã㯠Boolean.FalseStringãè¿ãã¾ãã |
Byte | Byte.ToString("G", NumberFormatInfo.CurrentInfo) ãå¼ã³åºãã¦ãç¾å¨ã®ã«ã«ãã£ã«åãã㦠Byte å¤ã®æ¸å¼è¨å®ããã¾ãã |
Char | æåãæååã¨ãã¦è¿ãã¾ãã |
DateTime | DateTime.ToString("G", DatetimeFormatInfo.CurrentInfo) ãå¼ã³åºãã¦ãç¾å¨ã®ã«ã«ãã£ã«åããã¦æ¥ä»ã¨æå»ã®å¤ã®æ¸å¼è¨å®ããã¾ãã |
Decimal | Decimal.ToString("G", NumberFormatInfo.CurrentInfo) ãå¼ã³åºãã¦ãç¾å¨ã®ã«ã«ãã£ã«åãã㦠Decimal å¤ã®æ¸å¼è¨å®ããã¾ãã |
Double | Double.ToString("G", NumberFormatInfo.CurrentInfo) ãå¼ã³åºãã¦ãç¾å¨ã®ã«ã«ãã£ã«åãã㦠Double å¤ã®æ¸å¼è¨å®ããã¾ãã |
Int16 | Int16.ToString("G", NumberFormatInfo.CurrentInfo) ãå¼ã³åºãã¦ãç¾å¨ã®ã«ã«ãã£ã«åãã㦠Int16 å¤ã®æ¸å¼è¨å®ããã¾ãã |
Int32 | Int32.ToString("G", NumberFormatInfo.CurrentInfo) ãå¼ã³åºãã¦ãç¾å¨ã®ã«ã«ãã£ã«åãã㦠Int32 å¤ã®æ¸å¼è¨å®ããã¾ãã |
Int64 | Int64.ToString("G", NumberFormatInfo.CurrentInfo) ãå¼ã³åºãã¦ãç¾å¨ã®ã«ã«ãã£ã«åãã㦠Int64 å¤ã®æ¸å¼è¨å®ããã¾ãã |
SByte | SByte.ToString("G", NumberFormatInfo.CurrentInfo) ãå¼ã³åºãã¦ãç¾å¨ã®ã«ã«ãã£ã«åãã㦠SByte å¤ã®æ¸å¼è¨å®ããã¾ãã |
Single | Single.ToString("G", NumberFormatInfo.CurrentInfo) ãå¼ã³åºãã¦ãç¾å¨ã®ã«ã«ãã£ã«åãã㦠Single å¤ã®æ¸å¼è¨å®ããã¾ãã |
UInt16 | UInt16.ToString("G", NumberFormatInfo.CurrentInfo) ãå¼ã³åºãã¦ãç¾å¨ã®ã«ã«ãã£ã«åãã㦠UInt16 å¤ã®æ¸å¼è¨å®ããã¾ãã |
UInt32 | UInt32.ToString("G", NumberFormatInfo.CurrentInfo) ãå¼ã³åºãã¦ãç¾å¨ã®ã«ã«ãã£ã«åãã㦠UInt32 å¤ã®æ¸å¼è¨å®ããã¾ãã |
UInt64 | UInt64.ToString("G", NumberFormatInfo.CurrentInfo) ãå¼ã³åºãã¦ãç¾å¨ã®ã«ã«ãã£ã«åãã㦠UInt64 å¤ã®æ¸å¼è¨å®ããã¾ãã |
ToString ã¡ã½ããã¨æ¸å¼æå®æåå
æ¢å®ã® ToString
ã¡ã½ããã ToString
ã®ãªã¼ãã¼ã©ã¤ãã®ä½¿ç¨ã¯ããªãã¸ã§ã¯ãã®æååå½¢å¼ã 1 ã¤ã®å ´åã«é©ãã¦ãã¾ãã ããããå¤ãã®å ´åãªãã¸ã§ã¯ãã®å¤ã«ã¯è¤æ°ã®å½¢å¼ãããã¾ãã ãã¨ãã°ã温度ã¯è¯æ°ãææ°ãã¾ãã¯ã±ã«ãã³ã§è¡¨ç¾ã§ãã¾ãã ã¾ããæ´æ°å¤ 10 㯠10ã10.0ã1.0e01ã$10.00 ãªã©ã®å¤ãã®æ¹æ³ã§è¡¨ããã¨ãã§ãã¾ãã
.NET ã§ã¯ãæ¸å¼æå®æååã使ç¨ãããã¨ã§ã1 ã¤ã®å¤ã«è¤æ°ã®æååå½¢å¼ãæããããã¨ãã§ãã¾ãã æ¸å¼æå®æååã¨ã¯å®ç¾©æ¸ã¿ã®æ¸å¼æå®åã 1 ã¤ä»¥ä¸å«ãæååã§ããæ¸å¼æå®åã¨ã¯ã ToString
ã¡ã½ããã«ãããã®åºåã®æ¸å¼è¨å®æ¹æ³ãå®ç¾©ãã 1 æåã¾ãã¯æåã°ã«ã¼ãã§ãã æ¸å¼æå®æååã¯ãã©ã¡ã¼ã¿ã¼ã¨ãã¦ãªãã¸ã§ã¯ãã® ToString
ã¡ã½ããã«æ¸¡ããããªãã¸ã§ã¯ãã®å¤ã®æååå½¢å¼ã®è¡¨ç¤ºæ¹æ³ã決å®ãã¾ãã
.NET ã§ã¯ããã¹ã¦ã®æ°å¤åãæ¥ä»/æå»åãããã³åæåã§ãå®ç¾©æ¸ã¿ã®ä¸é£ã®æ¸å¼æå®åããµãã¼ããã¦ãã¾ãã æ¸å¼æå®æååã使ç¨ãã¦ãã¢ããªã±ã¼ã·ã§ã³ã§å®ç¾©ããããã¼ã¿åã®æååå½¢å¼ãè¤æ°å®ç¾©ãããã¨ãã§ãã¾ãã
æ¨æºã®æ¸å¼æå®æåå
æ¨æºæ¸å¼æå®æååã«ã¯ãé©ç¨å ã®ãªãã¸ã§ã¯ãã®æååå½¢å¼ãå®ç¾©ããè±åã®åä¸ã®æ¸å¼æå®åã¨ãçµææååã«è¡¨ç¤ºãããæ¡æ°ã«å½±é¿ãããªãã·ã§ã³ã®ç²¾åº¦æå®åãå«ã¾ãã¾ãã 精度æå®åãçç¥ãããããµãã¼ãããã¦ããªãå ´åãæ¨æºæ¸å¼æå®åã¯æ¨æºæ¸å¼æå®æååã¨åãã«ãªãã¾ãã
.NET ã§ã¯ããã¹ã¦ã®æ°å¤åããã¹ã¦ã®æ¥ä»/æå»åãããã³ãã¹ã¦ã®åæåã«å¯¾ãã¦ä¸é£ã®æ¨æºæ¸å¼æå®åãå®ç¾©ããã¦ãã¾ãã ãã¨ãã°ããããã®ã«ãã´ãªãã¨ã«ã対å¿ããåã®å¤ã®ä¸è¬çãªæååå½¢å¼ãå®ç¾©ãã "G" æ¨æºæ¸å¼æå®åããµãã¼ãããã¦ãã¾ãã
åæåã®æ¨æºæ¸å¼æå®æååã¯ãå¤ã®æååå½¢å¼ãç´æ¥å¶å¾¡ãã¾ãã åæå¤ã® ToString
ã¡ã½ããã«æ¸¡ãããæ¸å¼æå®æååã«ãã£ã¦ãæååå ("G" æ¸å¼æå®åããã³ "F" æ¸å¼æå®å)ãåºã«ãªãæ´æ°å¤ ("D" æ¸å¼æå®å)ãã¾ã㯠16 é²å¤ ("X" æ¸å¼æå®å) ã®ãããã使ç¨ãã¦å¤ã表示ããããæ±ºå®ããã¾ãã æ¬¡ã®ã³ã¼ãä¾ã§ã¯ãæ¨æºæ¸å¼æå®æååã使ç¨ãã¦ã DayOfWeek åæå¤ã®æ¸å¼ãè¨å®ããæ¹æ³ã示ãã¦ãã¾ãã
DayOfWeek thisDay = DayOfWeek.Monday;
string[] formatStrings = {"G", "F", "D", "X"};
foreach (string formatString in formatStrings)
Console.WriteLine(thisDay.ToString(formatString));
// The example displays the following output:
// Monday
// Monday
// 1
// 00000001
Dim thisDay As DayOfWeek = DayOfWeek.Monday
Dim formatStrings() As String = {"G", "F", "D", "X"}
For Each formatString As String In formatStrings
Console.WriteLine(thisDay.ToString(formatString))
Next
' The example displays the following output:
' Monday
' Monday
' 1
' 00000001
åæåæ¸å¼æå®æååã«ã¤ãã¦ã¯ãã åæåæ¸å¼æå®æååããåç §ãã¦ãã ããã
æ°å¤åã®æ¨æºæ¸å¼æå®æååã¯ãé常ã表示ãããæ¡æ°ã 1 ã¤ä»¥ä¸ã®ããããã£å¤ã«ãã£ã¦å¶å¾¡ãããçµææååãå®ç¾©ãã¾ãã ãã¨ãã°ã"C" æ¸å¼æå®åã¯æ°åãé貨å¤ã¨ãã¦æ¸å¼è¨å®ãã¾ãã å¯ä¸ã®ãã©ã¡ã¼ã¿ã¼ã¨ã㦠"C" æ¸å¼æå®åãæ¸¡ã㦠ToString
ã¡ã½ãããå¼ã³åºããå ´åãç¾å¨ã®ã«ã«ãã£ã® NumberFormatInfo ãªãã¸ã§ã¯ãã®æ¬¡ã®ããããã£å¤ã使ç¨ãã¦æ°å¤ã®æååå½¢å¼ãå®ç¾©ãã¾ãã
CurrencySymbol ããããã£ãç¾å¨ã®ã«ã«ãã£ã®é貨è¨å·ãæå®ãã¾ãã
CurrencyNegativePattern ããããã£ã¾ã㯠CurrencyPositivePattern ããããã£ãæ¬¡ã®æ å ±ãç¹å®ããæ´æ°ãè¿ãã¾ãã
é貨è¨å·ã®ä½ç½®ã
è² ã®å¤ã表ãããã«ãå é ã®è² ã®ç¬¦å·ãæ«å°¾ã®è² ã®ç¬¦å·ãã¾ãã¯ãã£ãã®ã©ãã使ç¨ãããã
æ°å¤ã¨é貨è¨å·ã®éã«ã¹ãã¼ã¹æåã表示ãããã©ããã
CurrencyDecimalDigits ããããã£ãçµææååã®å°æ°ç¹ä»¥ä¸ã®æ¡æ°ãå®ç¾©ãã¾ãã
CurrencyDecimalSeparator ããããã£ãçµææååã®å°æ°ç¹ã®è¨å·ãå®ç¾©ãã¾ãã
CurrencyGroupSeparator ããããã£ãæ¡åºåãè¨å·ãå®ç¾©ãã¾ãã
CurrencyGroupSizes ããããã£ã¯ãå°æ°ç¹ã®å·¦å´ã«ããåã°ã«ã¼ãã®æ¡æ°ãå®ç¾©ãã¾ãã
NegativeSign ããããã£ããã£ãã使ç¨ããã«è² ã®å¤ã表ãå ´åã«çµææååã§ä½¿ç¨ããè² ã®ç¬¦å·ã決å®ãã¾ãã
ããã«ãæ°å¤æ¸å¼æå®æååã«ã¯ã精度æå®åãå«ã¾ããå ´åãããã¾ãã ãã®æå®åã®æå³ã¯ä¸ç·ã«ä½¿ç¨ãããæ¸å¼æå®æååã«ãã£ã¦ç°ãªãã¾ãããé常ã¯ãçµææååã«è¡¨ç¤ºãããåè¨æ¡æ°ãå°æ°ç¹ä»¥ä¸ã®æ¡æ°ã示ãã¾ãã ãã¨ãã°ã次ã®ä¾ã§ã¯ã"X4" ã®æ¨æºæ°å¤æååã¨ç²¾åº¦æå®åã使ç¨ãã¦ã4 æ¡ã® 16 鲿°ããæãæååå¤ã使ãã¾ãã
byte[] byteValues = { 12, 163, 255 };
foreach (byte byteValue in byteValues)
Console.WriteLine(byteValue.ToString("X4"));
// The example displays the following output:
// 000C
// 00A3
// 00FF
Dim byteValues() As Byte = {12, 163, 255}
For Each byteValue As Byte In byteValues
Console.WriteLine(byteValue.ToString("X4"))
Next
' The example displays the following output:
' 000C
' 00A3
' 00FF
æ¨æºã®æ°å¤æ¸å¼æå®æååã®è©³ç´°ã«ã¤ãã¦ã¯ãã æ¨æºã®æ°å¤æ¸å¼æå®æååããåç §ãã¦ãã ããã
æ¥ä»ã¨æå»ã®å¤ã®æ¨æºæ¸å¼æå®æååã¯ãç¹å®ã® DateTimeFormatInfo ããããã£ã«æ ¼ç´ããã¦ããã«ã¹ã¿ã æ¸å¼æå®æååã®ã¨ã¤ãªã¢ã¹ã§ãã ãã¨ãã°ã"D" æ¸å¼æå®åãæ¸¡ãã¦æ¥ä»ã¨æå»ã®å¤ã® ToString
ã¡ã½ãããå¼ã³åºãã¨ãç¾å¨ã®ã«ã«ãã£ã® DateTimeFormatInfo.LongDatePattern ããããã£ã«æ ¼ç´ããã¦ããã«ã¹ã¿ã æ¸å¼æå®æååã使ç¨ãã¦æ¥ä»ã¨æå»ã表示ããã¾ãã (ã«ã¹ã¿ã æ¸å¼æå®æååã®è©³ç´°ã«ã¤ãã¦ã¯ã次ã®ã»ã¯ã·ã§ã³ãåç
§ãã¦ãã ãã)ããã®é¢ä¿ã次ã®ä¾ã«ç¤ºãã¾ãã
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
DateTime date1 = new DateTime(2009, 6, 30);
Console.WriteLine($"D Format Specifier: {date1:D}");
string longPattern = CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern;
Console.WriteLine($"'{longPattern}' custom format string: {date1.ToString(longPattern)}");
}
}
// The example displays the following output when run on a system whose
// current culture is en-US:
// D Format Specifier: Tuesday, June 30, 2009
// 'dddd, MMMM dd, yyyy' custom format string: Tuesday, June 30, 2009
Imports System.Globalization
Module Example
Public Sub Main0()
Dim date1 As Date = #6/30/2009#
Console.WriteLine("D Format Specifier: {0:D}", date1)
Dim longPattern As String = CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern
Console.WriteLine("'{0}' custom format string: {1}",
longPattern, date1.ToString(longPattern))
End Sub
End Module
' The example displays the following output when run on a system whose
' current culture is en-US:
' D Format Specifier: Tuesday, June 30, 2009
' 'dddd, MMMM dd, yyyy' custom format string: Tuesday, June 30, 2009
æ¨æºã®æ¥ææ¸å¼æå®æååã®è©³ç´°ã«ã¤ãã¦ã¯ãã æ¨æºã®æ¥æå½¢å¼æååããåç §ãã¦ãã ããã
ã¾ããæ¨æºæ¸å¼æå®æååã使ç¨ãã¦ããªãã¸ã§ã¯ãã® ToString(String)
ã¡ã½ããã«ãã£ã¦çæããããã¢ããªã±ã¼ã·ã§ã³å®ç¾©ã®ãªãã¸ã§ã¯ãã®æååå½¢å¼ãå®ç¾©ãããã¨ãã§ãã¾ãã ãªãã¸ã§ã¯ãã§ãµãã¼ãããç¹å®ã®æ¨æºæ¸å¼æå®åãå®ç¾©ãããããããã§å¤§æåã¨å°æåãåºå¥ãããããªãããæ±ºå®ããããããã¨ãã§ãã¾ãã ToString(String)
ã¡ã½ããã®å®è£
ã§ã以ä¸ããµãã¼ãããã¾ãã
ãªãã¸ã§ã¯ãã®ä¸è¬çãªæ¸å¼ã¾ãã¯å ±éã®æ¸å¼ã表ã "G" æ¸å¼æå®åã ãªãã¸ã§ã¯ãã®
ToString
ã¡ã½ããã®ãã©ã¡ã¼ã¿ã¼ãªãã®ãªã¼ãã¼ãã¼ãã§ããã®ToString(String)
ãªã¼ãã¼ãã¼ããå¼ã³åºããããã« "G" æ¨æºæ¸å¼æå®æååãæ¸¡ãã¾ããnull åç § (Visual Basic ã®å ´åã¯
Nothing
) ã«ç¸å½ããæ¸å¼æå®åã null åç §ã«ç¸å½ããæ¸å¼æå®å㯠"G" æ¸å¼æå®åã¨åçã«æ±ãå¿ è¦ãããã¾ãã
ãã¨ãã°ã Temperature
ã¯ã©ã¹ã®å ´åãå
é¨çã«ã¯æ¸©åº¦ãææ°ã§æ ¼ç´ããæ¸å¼æå®åã使ç¨ã㦠Temperature
ãªãã¸ã§ã¯ãã®å¤ãææ°ãè¯æ°ãããã³ã±ã«ãã³ã§è¡¨ããã¨ãã§ãã¾ãã å
·ä½çãªä¾ã次ã«ç¤ºãã¾ãã
using System;
public class Temperature
{
private decimal m_Temp;
public Temperature(decimal temperature)
{
this.m_Temp = temperature;
}
public decimal Celsius
{
get { return this.m_Temp; }
}
public decimal Kelvin
{
get { return this.m_Temp + 273.15m; }
}
public decimal Fahrenheit
{
get { return Math.Round(((decimal) (this.m_Temp * 9 / 5 + 32)), 2); }
}
public override string ToString()
{
return this.ToString("C");
}
public string ToString(string format)
{
// Handle null or empty string.
if (String.IsNullOrEmpty(format)) format = "C";
// Remove spaces and convert to uppercase.
format = format.Trim().ToUpperInvariant();
// Convert temperature to Fahrenheit and return string.
switch (format)
{
// Convert temperature to Fahrenheit and return string.
case "F":
return this.Fahrenheit.ToString("N2") + " °F";
// Convert temperature to Kelvin and return string.
case "K":
return this.Kelvin.ToString("N2") + " K";
// return temperature in Celsius.
case "G":
case "C":
return this.Celsius.ToString("N2") + " °C";
default:
throw new FormatException(String.Format("The '{0}' format string is not supported.", format));
}
}
}
public class Example1
{
public static void Main()
{
Temperature temp1 = new Temperature(0m);
Console.WriteLine(temp1.ToString());
Console.WriteLine(temp1.ToString("G"));
Console.WriteLine(temp1.ToString("C"));
Console.WriteLine(temp1.ToString("F"));
Console.WriteLine(temp1.ToString("K"));
Temperature temp2 = new Temperature(-40m);
Console.WriteLine(temp2.ToString());
Console.WriteLine(temp2.ToString("G"));
Console.WriteLine(temp2.ToString("C"));
Console.WriteLine(temp2.ToString("F"));
Console.WriteLine(temp2.ToString("K"));
Temperature temp3 = new Temperature(16m);
Console.WriteLine(temp3.ToString());
Console.WriteLine(temp3.ToString("G"));
Console.WriteLine(temp3.ToString("C"));
Console.WriteLine(temp3.ToString("F"));
Console.WriteLine(temp3.ToString("K"));
Console.WriteLine(String.Format("The temperature is now {0:F}.", temp3));
}
}
// The example displays the following output:
// 0.00 °C
// 0.00 °C
// 0.00 °C
// 32.00 °F
// 273.15 K
// -40.00 °C
// -40.00 °C
// -40.00 °C
// -40.00 °F
// 233.15 K
// 16.00 °C
// 16.00 °C
// 16.00 °C
// 60.80 °F
// 289.15 K
// The temperature is now 16.00 °C.
Public Class Temperature
Private m_Temp As Decimal
Public Sub New(temperature As Decimal)
Me.m_Temp = temperature
End Sub
Public ReadOnly Property Celsius() As Decimal
Get
Return Me.m_Temp
End Get
End Property
Public ReadOnly Property Kelvin() As Decimal
Get
Return Me.m_Temp + 273.15D
End Get
End Property
Public ReadOnly Property Fahrenheit() As Decimal
Get
Return Math.Round(CDec(Me.m_Temp * 9 / 5 + 32), 2)
End Get
End Property
Public Overrides Function ToString() As String
Return Me.ToString("C")
End Function
Public Overloads Function ToString(format As String) As String
' Handle null or empty string.
If String.IsNullOrEmpty(format) Then format = "C"
' Remove spaces and convert to uppercase.
format = format.Trim().ToUpperInvariant()
Select Case format
Case "F"
' Convert temperature to Fahrenheit and return string.
Return Me.Fahrenheit.ToString("N2") & " °F"
Case "K"
' Convert temperature to Kelvin and return string.
Return Me.Kelvin.ToString("N2") & " K"
Case "C", "G"
' Return temperature in Celsius.
Return Me.Celsius.ToString("N2") & " °C"
Case Else
Throw New FormatException(String.Format("The '{0}' format string is not supported.", format))
End Select
End Function
End Class
Public Module Example1
Public Sub Main1()
Dim temp1 As New Temperature(0D)
Console.WriteLine(temp1.ToString())
Console.WriteLine(temp1.ToString("G"))
Console.WriteLine(temp1.ToString("C"))
Console.WriteLine(temp1.ToString("F"))
Console.WriteLine(temp1.ToString("K"))
Dim temp2 As New Temperature(-40D)
Console.WriteLine(temp2.ToString())
Console.WriteLine(temp2.ToString("G"))
Console.WriteLine(temp2.ToString("C"))
Console.WriteLine(temp2.ToString("F"))
Console.WriteLine(temp2.ToString("K"))
Dim temp3 As New Temperature(16D)
Console.WriteLine(temp3.ToString())
Console.WriteLine(temp3.ToString("G"))
Console.WriteLine(temp3.ToString("C"))
Console.WriteLine(temp3.ToString("F"))
Console.WriteLine(temp3.ToString("K"))
Console.WriteLine(String.Format("The temperature is now {0:F}.", temp3))
End Sub
End Module
' The example displays the following output:
' 0.00 °C
' 0.00 °C
' 0.00 °C
' 32.00 °F
' 273.15 K
' -40.00 °C
' -40.00 °C
' -40.00 °C
' -40.00 °F
' 233.15 K
' 16.00 °C
' 16.00 °C
' 16.00 °C
' 60.80 °F
' 289.15 K
' The temperature is now 16.00 °C.
ã«ã¹ã¿ã æ¸å¼è¨å®æåå
.NET ã§ã¯ãæ¨æºæ¸å¼æå®æååã®ã»ãã«ãæ°å¤ããã³æ¥ä»ã¨æå»ã®å¤ã®ä¸¡æ¹ã®ã«ã¹ã¿ã æ¸å¼æå®æååãå®ç¾©ããã¦ãã¾ãã ã«ã¹ã¿ã æ¸å¼æå®æååã¯ãå¤ã®æååå½¢å¼ãå®ç¾©ãã 1 ã¤ä»¥ä¸ã®ã«ã¹ã¿ã æ¸å¼æå®åã§æ§æããã¾ãã ãã¨ãã°ã"yyyy/mm/dd hh:mm:ss.ffff t zzz" ã¨ããã«ã¹ã¿ã æ¥ææ¸å¼æå®æååã®å ´åãen-US ã«ã«ãã£ã§ã¯æ¥ä»ã "2008/11/15 07:45:00.0000 P -08:00" ã¨ããæååå½¢å¼ã«å¤æããã¾ãã ã¾ãã"0000" ã¨ããã«ã¹ã¿ã æ¸å¼æå®æååã®å ´åãæ´æ°å¤ 12 㯠"0012" ã«å¤æããã¾ãã ã«ã¹ã¿ã æ¸å¼æå®æååã®ä¸è¦§ã«ã¤ãã¦ã¯ãã ã«ã¹ã¿ã æ¥æå½¢å¼æåå ãããã³ã ã«ã¹ã¿ã æ°å¤å½¢å¼æååããåç §ãã¦ãã ããã
æ¸å¼æå®æååãåä¸ã®ã«ã¹ã¿ã æ¸å¼æå®åã§æ§æãããå ´åã¯ãæ¨æºæ¸å¼æå®åã¨æ··åããªãããã«ãæ¸å¼æå®åã®åã«ãã¼ã»ã³ã (%) è¨å·ãä»ããå¿ è¦ãããã¾ãã æ¬¡ã®ä¾ã§ã¯ã"M" ã«ã¹ã¿ã æ¸å¼æå®åã使ç¨ãã¦ãç¹å®ã®æ¥ä»ã®æã表ã 1 æ¡ã¾ã㯠2 æ¡ã®æ°å¤ã表示ãã¾ãã
DateTime date1 = new DateTime(2009, 9, 8);
Console.WriteLine(date1.ToString("%M")); // Displays 9
Dim date1 As Date = #09/08/2009#
Console.WriteLine(date1.ToString("%M")) ' Displays 9
æ¥ä»ããã³æå»ã®å¤ã®æ¨æºæ¸å¼æå®æååã®å¤ãã¯ã DateTimeFormatInfo ãªãã¸ã§ã¯ãã®ããããã£ã«ãã£ã¦å®ç¾©ããã¦ããã«ã¹ã¿ã æ¸å¼æå®æååã®ã¨ã¤ãªã¢ã¹ã§ãã ã¾ããã«ã¹ã¿ã æ¸å¼æå®æååã使ç¨ãããã¨ã§ãæ°å¤ã¾ãã¯æ¥ä»ã¨æå»ã®å¤ã«å¯¾ãã¦ãæè»ã«ã¢ããªã±ã¼ã·ã§ã³å®ç¾©ã®æ¸å¼ãæå®ã§ãã¾ãã è¤æ°ã®ã«ã¹ã¿ã æ¸å¼æå®åã 1 ã¤ã®ã«ã¹ã¿ã æ¸å¼æå®æååã«çµåãããã¨ã«ãã£ã¦ãæ°å¤ããã³æ¥ä»ã¨æå»ã®å¤ã®ä¸¡æ¹ã«å¯¾ããã«ã¹ã¿ã ã®çµææååãç¬èªã«å®ç¾©ãããã¨ãã§ãã¾ãã æ¬¡ã®ä¾ã§ã¯ãæã®ååãæ¥ä»ãããã³å¹´ã®å¾ã«ããã£ãã§å²ãã§ææ¥ã表示ããã«ã¹ã¿ã æ¸å¼æå®æååãå®ç¾©ãã¦ãã¾ãã
string customFormat = "MMMM dd, yyyy (dddd)";
DateTime date1 = new DateTime(2009, 8, 28);
Console.WriteLine(date1.ToString(customFormat));
// The example displays the following output if run on a system
// whose language is English:
// August 28, 2009 (Friday)
Dim customFormat As String = "MMMM dd, yyyy (dddd)"
Dim date1 As Date = #8/28/2009#
Console.WriteLine(date1.ToString(customFormat))
' The example displays the following output if run on a system
' whose language is English:
' August 28, 2009 (Friday)
次ã®ä¾ã§ã¯ãç±³å½ã®æ¨æºçãª7æ¡ã®é»è©±çªå·ã¨ãã¦ãInt64 ã®å¤ãå¸å¤å±çªã¨å ±ã«è¡¨ç¤ºããã«ã¹ã¿ã æ¸å¼ãå®ç¾©ãã¾ãã
using System;
public class Example17
{
public static void Main()
{
long number = 8009999999;
string fmt = "000-000-0000";
Console.WriteLine(number.ToString(fmt));
}
}
// The example displays the following output:
// 800-999-9999
Module Example18
Public Sub Main18()
Dim number As Long = 8009999999
Dim fmt As String = "000-000-0000"
Console.WriteLine(number.ToString(fmt))
End Sub
End Module
' The example displays the following output:
' The example displays the following output:
' 800-999-9999
ä¸è¬ã«ã¯ãã¢ããªã±ã¼ã·ã§ã³å®ç¾©ã®åã«å¯¾ããæ¸å¼è¨å®ã®ã»ã¨ãã©ã®ãã¼ãºã«æ¨æºæ¸å¼æå®æååã使ç¨ãã¦å¯¾å¿ã§ãã¾ãããã«ã¹ã¿ã æ¸å¼æå®åãå®ç¾©ãã¦åã®æ¸å¼ãè¨å®ãããã¨ãã§ãã¾ãã
æ¸å¼æå®æåå㨠.NET å
ãã¹ã¦ã®æ°å¤å (ã¤ã¾ããByteãDecimalãDoubleãInt16ãInt32ãInt64ãSByteãSingleãUInt16ãUInt32ãUInt64ãããã³ BigInteger å)ãDateTimeãDateTimeOffsetãTimeSpanãGuidããã¹ã¦ã®åæåããæ¸å¼æå®æååã«ããæ¸å¼è¨å®ã«å¯¾å¿ãã¦ãã¾ãã ååã§ãµãã¼ãããã¦ããç¹å®ã®æ¸å¼æå®æååã«ã¤ãã¦ã¯ã次ã®ãããã¯ãåç §ãã¦ãã ããã
ã¿ã¤ãã« | å®ç¾© |
---|---|
æ¨æºã®æ°å¤æ¸å¼æå®æåå | æ°å¤ã®ä¸è¬çãªæåå表ç¾ãçæããæ¨æºæ¸å¼æååã«ã¤ãã¦èª¬æãã¾ãã |
ã«ã¹ã¿ã æ°å¤å½¢å¼æåå | æ°å¤ã«å¯¾ããã¢ããªã±ã¼ã·ã§ã³åºæã®æååå½¢å¼ã使ããã«ã¹ã¿ã æ¸å¼æå®æååã«ã¤ãã¦èª¬æãã¾ãã |
æ¨æºã®æ¥æå½¢å¼æåå | DateTime 㨠DateTimeOffset å¤ã«å¯¾ãã¦ä¸è¬çã«ä½¿ç¨ãããæååå½¢å¼ã使ããæ¨æºæ¸å¼æå®æååã«ã¤ãã¦èª¬æãã¾ãã |
ã«ã¹ã¿ã æ¥æå½¢å¼æåå | DateTime 㨠DateTimeOffset å¤ã«å¯¾ããã¢ããªã±ã¼ã·ã§ã³åºæã®æååå½¢å¼ã使ããã«ã¹ã¿ã æ¸å¼æå®æååã«ã¤ãã¦èª¬æãã¾ãã |
æ¨æºã®æéé鿏弿宿åå | æéééã«å¯¾ãã¦ä¸è¬çã«ä½¿ç¨ãããæååå½¢å¼ã使ããæ¨æºæ¸å¼æå®æååã«ã¤ãã¦èª¬æãã¾ãã |
ã«ã¹ã¿ã æéé鿏弿宿åå | æéééã«å¯¾ããã¢ããªã±ã¼ã·ã§ã³åºæã®æååå½¢å¼ã使ããã«ã¹ã¿ã æ¸å¼æå®æååã«ã¤ãã¦èª¬æãã¾ãã |
åæåæ¸å¼æå®æåå | åæåã®å¤ã®æåå表ç¾ã使ããããã«ä½¿ç¨ãããæ¨æºãã©ã¼ãããæååã«ã¤ãã¦èª¬æãã¾ãã |
Guid.ToString(String) | Guid å¤ã®æ¨æºçæ¸å¼æå®æååã«ã¤ãã¦èª¬æãã¾ãã |
æ¸å¼ãããã¤ãã¼ã«ããã«ã«ãã£ã«ä¾åããæ¸å¼è¨å®
æ¸å¼æå®åãå©ç¨ãããã¨ã§ãªãã¸ã§ã¯ãã®æ¸å¼ãã«ã¹ã¿ãã¤ãºã§ãã¾ãããå¤ãã®å ´åãæå³ã®ãããªãã¸ã§ã¯ãã®æååå½¢å¼ãçæããã«ã¯è¿½å ã®æ¸å¼è¨å®æ
å ±ãå¿
è¦ã§ãã ãã¨ãã°ã"C" æ¨æºæ¸å¼æå®æååã¾ã㯠"$ #,#.00" ãªã©ã®ã«ã¹ã¿ã æ¸å¼æå®æååã使ç¨ãã¦æ°åãé貨å¤ã¨ãã¦æ¸å¼è¨å®ããå ´åãå°ãªãã¨ããæ£ããé貨è¨å·ãæ¡åºåãè¨å·ãããã³å°æ°ç¹è¨å·ã«ã¤ãã¦ã®æ
å ±ãæ¸å¼è¨å®ãããæååã«å«ãããã¨ãã§ããå¿
è¦ãããã¾ãã .NET ã§ã¯ãIFormatProvider ã¤ã³ã¿ã¼ãã§ã¤ã¹ã«ãã£ã¦ããã®è¿½å ã®æ¸å¼è¨å®æ
å ±ãå©ç¨ã§ããããã«ãã¾ãããã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ã¯ãæ°å¤åããã³æ¥ä»/æå»åã® ToString
ã¡ã½ããã® 1 ã¤ä»¥ä¸ã®ãªã¼ãã¼ãã¼ãã«å¯¾ãããã©ã¡ã¼ã¿ã¼ã¨ãã¦æä¾ããã¾ãã IFormatProvider ã®å®è£
㯠.NET ã§ä½¿ç¨ãããã«ã«ãã£åºæã®æ¸å¼æå®ããµãã¼ããã¾ãã ããããç°ãªãã«ã«ãã£ã示ã 3 ã¤ã® IFormatProvider ãªãã¸ã§ã¯ãã使ç¨ãã¦ãªãã¸ã§ã¯ãã®æ¸å¼ãè¨å®ããå ´åã«ããã®æååå½¢å¼ãã©ã®ããã«å¤åããããæ¬¡ã®ä¾ã«ç¤ºãã¾ãã
using System;
using System.Globalization;
public class Example18
{
public static void Main()
{
decimal value = 1603.42m;
Console.WriteLine(value.ToString("C3", new CultureInfo("en-US")));
Console.WriteLine(value.ToString("C3", new CultureInfo("fr-FR")));
Console.WriteLine(value.ToString("C3", new CultureInfo("de-DE")));
}
}
// The example displays the following output:
// $1,603.420
// 1 603,420 â¬
// 1.603,420 â¬
Imports System.Globalization
Public Module Example11
Public Sub Main11()
Dim value As Decimal = 1603.42D
Console.WriteLine(value.ToString("C3", New CultureInfo("en-US")))
Console.WriteLine(value.ToString("C3", New CultureInfo("fr-FR")))
Console.WriteLine(value.ToString("C3", New CultureInfo("de-DE")))
End Sub
End Module
' The example displays the following output:
' $1,603.420
' 1 603,420 â¬
' 1.603,420 â¬
IFormatProvider ã¤ã³ã¿ã¼ãã§ã¤ã¹ã«ã¯ã GetFormat(Type)ã¨ãã 1 ã¤ã®ã¡ã½ãããå«ã¾ãã¦ãã¾ãããã®ã¡ã½ããã«ã¯ãæ¸å¼è¨å®æ
å ±ãæä¾ãããªãã¸ã§ã¯ãã®åãæå®ãã 1 ã¤ã®ãã©ã¡ã¼ã¿ã¼ãããã¾ãã ãã®ã¡ã½ããããã®åã®ãªãã¸ã§ã¯ããæä¾ã§ããå ´åã¯ããããè¿ããã¾ãã ãã以å¤ã®å ´åã¯ãnull åç
§ (Visual Basic ã®å ´åã¯Nothing
) ãè¿ããã¾ãã
IFormatProvider.GetFormat ã¯ã³ã¼ã«ãã㯠ã¡ã½ããã§ãã ToString
ãã©ã¡ã¼ã¿ã¼ãå«ã IFormatProvider ã¡ã½ãã ãªã¼ãã¼ãã¼ããå¼ã³åºãã¨ããã® GetFormat ãªãã¸ã§ã¯ãã® IFormatProvider ã¡ã½ãããå¼ã³åºããã¾ãã GetFormat ã¡ã½ããã¯ã formatType
ãã©ã¡ã¼ã¿ã¼ã§æå®ããããå¿
è¦ãªæ¸å¼è¨å®æ
å ±ãæä¾ãããªãã¸ã§ã¯ãã ToString
ã¡ã½ããã«è¿ãã¾ãã
æ°å¤ãã®æ¸å¼æå®ã¡ã½ãããæåå夿ã¡ã½ããã« IFormatProvideråã®ãã©ã¡ã¼ã¿ã¼ãå«ã¾ãã¦ãã¾ãããå¤ãã®å ´åãã¡ã½ãããå¼ã³åºãã¨ãã¯ãã©ã¡ã¼ã¿ã¼ã®å¤ã¯ç¡è¦ããã¾ãã Type ã¡ã½ããã«æ¸¡ããã IFormatProvider.GetFormat ãªãã¸ã§ã¯ãã®ãã©ã¡ã¼ã¿ã¼ã¨åã使ç¨ããæ¸å¼æå®ã¡ã½ããã®ä¸é¨ã次ã®è¡¨ã«ç¤ºãã¾ãã
ã¡ã½ãã | formatType ãã©ã¡ã¼ã¿ã¼ã®å |
---|---|
æ°å¤åã®ToString ã¡ã½ãã |
System.Globalization.NumberFormatInfo |
æ¥ä»/æå»åã®ToString ã¡ã½ãã |
System.Globalization.DateTimeFormatInfo |
String.Format | System.ICustomFormatter |
StringBuilder.AppendFormat | System.ICustomFormatter |
注
æ°å¤åããã³æ¥ä»/æå»åã® ToString
ã¡ã½ããã¯ãªã¼ãã¼ãã¼ãããã¾ããã IFormatProvider ãã©ã¡ã¼ã¿ã¼ãå«ã¾ããã®ã¯ãã®ãã¡ã®ä¸é¨ã®ãªã¼ãã¼ãã¼ãã ãã§ãã ã¡ã½ããã« IFormatProvideråã®ãã©ã¡ã¼ã¿ã¼ããªãå ´åã¯ã代ããã« CultureInfo.CurrentCulture ããããã£ã«ãã£ã¦è¿ããããªãã¸ã§ã¯ããæ¸¡ããã¾ãã ãã¨ãã°ãæ¢å®ã® Int32.ToString() ã¡ã½ããã®å¼ã³åºãã®å ´åã¯ãæçµçã«ã¯ Int32.ToString("G", System.Globalization.CultureInfo.CurrentCulture)
ã®ãããªã¡ã½ããå¼ã³åºãã«ãªãã¾ãã
.NET ã«ã¯ãIFormatProvider ãå®è£ ããæ¬¡ã® 3 ã¤ã®ã¯ã©ã¹ãç¨æããã¦ãã¾ãã
DateTimeFormatInfoããã®ã¯ã©ã¹ã¯ãç¹å®ã®ã«ã«ãã£ã®æ¥ä»ã¨æå»ã®å¤ã«å¯¾ããæ¸å¼è¨å®æ å ±ãæä¾ãã¾ãã 対å¿ãã IFormatProvider.GetFormat ã®å®è£ ã§ã¯ãããèªèº«ã®ã¤ã³ã¹ã¿ã³ã¹ãè¿ããã¾ãã
NumberFormatInfoããã®ã¯ã©ã¹ã¯ãç¹å®ã®ã«ã«ãã£ã®æ°å¤ã«å¯¾ããæ¸å¼è¨å®æ å ±ãæä¾ãã¾ãã 対å¿ãã IFormatProvider.GetFormat ã®å®è£ ã§ã¯ãããèªèº«ã®ã¤ã³ã¹ã¿ã³ã¹ãè¿ããã¾ãã
CultureInfoã 対å¿ãã IFormatProvider.GetFormat ã®å®è£ ã§ã¯ãæ°å¤ã«å¯¾ããæ¸å¼è¨å®æ å ±ãæä¾ãã NumberFormatInfo ãªãã¸ã§ã¯ãããæ¥ä»ã¨æå»ã®å¤ã«å¯¾ããæ¸å¼è¨å®æ å ±ãæä¾ãã DateTimeFormatInfo ãªãã¸ã§ã¯ãã®ãããããè¿ããã¾ãã
ã¾ãããããã®ã¯ã©ã¹ã®ãã¡ã®ãããããç½®ãæããç¬èªã®æ¸å¼ãããã¤ãã¼ãå®è£
ã§ãã¾ãã ãã ããå®è£
ã® GetFormat ã¡ã½ããã¯ãæ¸å¼è¨å®æ
å ±ã ToString
ã¡ã½ããã«æ¸¡ãå¿
è¦ãããå ´åãåã®è¡¨ã«ä¸è¦§ãããåã®ãªãã¸ã§ã¯ããè¿ãå¿
è¦ãããã¾ãã
æ°å¤ã«å¯¾ããæåã«é æ ®ããæ¸å¼è¨å®
æ¢å®ã§ã¯ãæ°å¤ã®æ¸å¼æå®ã¯ã«ã«ãã£ã«ä¾åãã¾ãã æ¸å¼æå®ã¡ã½ãããå¼ã³åºãã¨ãã«ã«ã«ãã£ãæå®ããªãå ´åã¯ãç¾å¨ã®ã«ã«ãã£ã®æ¸å¼æå®è¦åã使ç¨ããã¾ãã æ¬¡ã«ç¤ºãä¾ã§ã¯ãç¾å¨ã®ã«ã«ãã£ã 4 å夿´ããå¾ã«ãDecimal.ToString(String) ã¡ã½ãããå¼ã³åºãã¾ãã åã±ã¼ã¹ã§ã¯ãçµæã®æååã¯ãç¾å¨ã®ã«ã«ãã£ã®æ¸å¼æå®è¦åãåæ ãã¾ãã ããã¯ãåæ°å¤åã® ToString
ã¡ã½ããã¸ã®å¼ã³åºããã ToString(String)
ã¡ã½ãã㨠ToString(String, IFormatProvider)
ã¡ã½ãããã©ããããããã§ãã
using System.Globalization;
public class Example6
{
public static void Main()
{
string[] cultureNames = { "en-US", "fr-FR", "es-MX", "de-DE" };
Decimal value = 1043.17m;
foreach (var cultureName in cultureNames) {
// Change the current culture.
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName);
Console.WriteLine($"The current culture is {CultureInfo.CurrentCulture.Name}");
Console.WriteLine(value.ToString("C2"));
Console.WriteLine();
}
}
}
// The example displays the following output:
// The current culture is en-US
// $1,043.17
//
// The current culture is fr-FR
// 1 043,17 â¬
//
// The current culture is es-MX
// $1,043.17
//
// The current culture is de-DE
// 1.043,17 â¬
Imports System.Globalization
Module Example6
Public Sub Main6()
Dim cultureNames() As String = {"en-US", "fr-FR", "es-MX", "de-DE"}
Dim value As Decimal = 1043.17D
For Each cultureName In cultureNames
' Change the current culture.
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName)
Console.WriteLine($"The current culture is {CultureInfo.CurrentCulture.Name}")
Console.WriteLine(value.ToString("C2"))
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' The current culture is en-US
' $1,043.17
'
' The current culture is fr-FR
' 1 043,17 â¬
'
' The current culture is es-MX
' $1,043.17
'
' The current culture is de-DE
' 1.043,17 â¬
ã¾ãã ToString
ãã©ã¡ã¼ã¿ã¼ãæã¤ provider
ãªã¼ãã¼ãã¼ããå¼ã³åºãã¦ã次ã®ã©ã¡ãããæ¸¡ããã¨ã«ãããç¹å®ã«ã«ãã£ã®æ°å¤ãæ¸å¼æå®ãããã¨ãã§ãã¾ãã
使ç¨ãããæ¸å¼æå®è¦åã®ã«ã«ãã£ã表ã CultureInfo ãªãã¸ã§ã¯ãã ãã® CultureInfo.GetFormat ã¡ã½ããã¯ã CultureInfo.NumberFormat ããããã£ã®å¤ãè¿ãã¾ãããã®ããããã£ã¯ãæ°å¤ã«ã«ã«ãã£åºæã®æ¸å¼æå®æ å ±ãæä¾ãã NumberFormatInfo ãªãã¸ã§ã¯ãã§ãã
使ç¨ãããã«ã«ãã£åºæã®æ¸å¼æå®è¦åãå®ç¾©ãã NumberFormatInfo ãªãã¸ã§ã¯ãã ãã® GetFormat ã¡ã½ããã§ã¯ãããèªèº«ã®ã¤ã³ã¹ã¿ã³ã¹ãè¿ããã¾ãã
次ã®ä¾ã§ã¯ãæµ®åå°æ°ç¹æ°ãæ¸å¼æå®ããéã«ãè±èª (ç±³å½) ã¨è±èª (è±å½) ã®ã«ã«ãã£ããã³ãã©ã³ã¹èªã¨ãã·ã¢èªã®ãã¥ã¼ãã©ã« ã«ã«ãã£ã表ã NumberFormatInfo ãªãã¸ã§ã¯ãã使ç¨ãã¾ãã
using System.Globalization;
public class Example7
{
public static void Main()
{
double value = 1043.62957;
string[] cultureNames = { "en-US", "en-GB", "ru", "fr" };
foreach (string? name in cultureNames)
{
NumberFormatInfo nfi = CultureInfo.CreateSpecificCulture(name).NumberFormat;
Console.WriteLine("{0,-6} {1}", name + ":", value.ToString("N3", nfi));
}
}
}
// The example displays the following output:
// en-US: 1,043.630
// en-GB: 1,043.630
// ru: 1 043,630
// fr: 1 043,630
Imports System.Globalization
Module Example7
Public Sub Main7()
Dim value As Double = 1043.62957
Dim cultureNames() As String = {"en-US", "en-GB", "ru", "fr"}
For Each name In cultureNames
Dim nfi As NumberFormatInfo = CultureInfo.CreateSpecificCulture(name).NumberFormat
Console.WriteLine("{0,-6} {1}", name + ":", value.ToString("N3", nfi))
Next
End Sub
End Module
' The example displays the following output:
' en-US: 1,043.630
' en-GB: 1,043.630
' ru: 1 043,630
' fr: 1 043,630
æ¥ä»ã¨æå»ã®å¤ã®ã«ã«ãã£ã«ä¾åããæ¸å¼è¨å®
æ¢å®ã§ã¯ãæ¥æã®å¤ã®æ¸å¼æå®ã¯ã«ã«ãã£ã«ä¾åãã¾ãã æ¸å¼æå®ã¡ã½ãããå¼ã³åºãã¨ãã«ã«ã«ãã£ãæå®ããªãå ´åã¯ãç¾å¨ã®ã«ã«ãã£ã®æ¸å¼æå®è¦åã使ç¨ããã¾ãã æ¬¡ã«ç¤ºãä¾ã§ã¯ãç¾å¨ã®ã«ã«ãã£ã 4 å夿´ããå¾ã«ãDateTime.ToString(String) ã¡ã½ãããå¼ã³åºãã¾ãã åã±ã¼ã¹ã§ã¯ãçµæã®æååã¯ãç¾å¨ã®ã«ã«ãã£ã®æ¸å¼æå®è¦åãåæ ãã¾ãã ããã¯ã DateTime.ToString()ã DateTime.ToString(String)ã DateTimeOffset.ToString()ã DateTimeOffset.ToString(String) ã®åã¡ã½ãããã DateTime.ToString(String, IFormatProvider) ã¡ã½ããããã³ DateTimeOffset.ToString(String, IFormatProvider) ã¡ã½ããã¸ã®å¼ã³åºããã©ããããããã§ãã
using System.Globalization;
public class Example4
{
public static void Main()
{
string[] cultureNames = { "en-US", "fr-FR", "es-MX", "de-DE" };
DateTime dateToFormat = new DateTime(2012, 5, 28, 11, 30, 0);
foreach (var cultureName in cultureNames) {
// Change the current culture.
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName);
Console.WriteLine($"The current culture is {CultureInfo.CurrentCulture.Name}");
Console.WriteLine(dateToFormat.ToString("F"));
Console.WriteLine();
}
}
}
// The example displays the following output:
// The current culture is en-US
// Monday, May 28, 2012 11:30:00 AM
//
// The current culture is fr-FR
// lundi 28 mai 2012 11:30:00
//
// The current culture is es-MX
// lunes, 28 de mayo de 2012 11:30:00 a.m.
//
// The current culture is de-DE
// Montag, 28. Mai 2012 11:30:00
Imports System.Globalization
Imports System.Threading
Module Example4
Public Sub Main4()
Dim cultureNames() As String = {"en-US", "fr-FR", "es-MX", "de-DE"}
Dim dateToFormat As Date = #5/28/2012 11:30AM#
For Each cultureName In cultureNames
' Change the current culture.
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName)
Console.WriteLine($"The current culture is {CultureInfo.CurrentCulture.Name}")
Console.WriteLine(dateToFormat.ToString("F"))
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' The current culture is en-US
' Monday, May 28, 2012 11:30:00 AM
'
' The current culture is fr-FR
' lundi 28 mai 2012 11:30:00
'
' The current culture is es-MX
' lunes, 28 de mayo de 2012 11:30:00 a.m.
'
' The current culture is de-DE
' Montag, 28. Mai 2012 11:30:00
DateTime.ToString ãã©ã¡ã¼ã¿ã¼ãæã¤ DateTimeOffset.ToString ã¾ã㯠provider
ãªã¼ãã¼ãã¼ããå¼ã³åºãã¦ã次ã®ã©ã¡ãããæ¸¡ããã¨ã«ãããç¹å®ã«ã«ãã£ã®æ¥æã®å¤ãæ¸å¼æå®ãããã¨ãã§ãã¾ãã
使ç¨ãããæ¸å¼æå®è¦åã®ã«ã«ãã£ã表ã CultureInfo ãªãã¸ã§ã¯ãã ãã® CultureInfo.GetFormat ã¡ã½ããã¯ã CultureInfo.DateTimeFormat ããããã£ã®å¤ãè¿ãã¾ãããã®ããããã£ã¯ãæ¥æã®å¤ã«ã«ã«ãã£åºæã®æ¸å¼æå®æ å ±ãæä¾ãã DateTimeFormatInfo ãªãã¸ã§ã¯ãã§ãã
使ç¨ãããã«ã«ãã£åºæã®æ¸å¼æå®è¦åãå®ç¾©ãã DateTimeFormatInfo ãªãã¸ã§ã¯ãã ãã® GetFormat ã¡ã½ããã§ã¯ãããèªèº«ã®ã¤ã³ã¹ã¿ã³ã¹ãè¿ããã¾ãã
次ã®ä¾ã§ã¯ãæ¥ä»ãæ¸å¼æå®ããéã«ãè±èª (ç±³å½) ã¨è±èª (è±å½) ã®ã«ã«ãã£ããã³ãã©ã³ã¹èªã¨ãã·ã¢èªã®ãã¥ã¼ãã©ã« ã«ã«ãã£ã表ã DateTimeFormatInfo ãªãã¸ã§ã¯ãã使ç¨ãã¾ãã
using System.Globalization;
public class Example5
{
public static void Main()
{
DateTime dat1 = new(2012, 5, 28, 11, 30, 0);
string[] cultureNames = { "en-US", "en-GB", "ru", "fr" };
foreach (var name in cultureNames) {
DateTimeFormatInfo dtfi = CultureInfo.CreateSpecificCulture(name).DateTimeFormat;
Console.WriteLine($"{name}: {dat1.ToString(dtfi)}");
}
}
}
// The example displays the following output:
// en-US: 5/28/2012 11:30:00 AM
// en-GB: 28/05/2012 11:30:00
// ru: 28.05.2012 11:30:00
// fr: 28/05/2012 11:30:00
Imports System.Globalization
Module Example5
Public Sub Main5()
Dim dat1 As Date = #5/28/2012 11:30AM#
Dim cultureNames() As String = {"en-US", "en-GB", "ru", "fr"}
For Each name In cultureNames
Dim dtfi As DateTimeFormatInfo = CultureInfo.CreateSpecificCulture(name).DateTimeFormat
Console.WriteLine($"{name}: {dat1.ToString(dtfi)}")
Next
End Sub
End Module
' The example displays the following output:
' en-US: 5/28/2012 11:30:00 AM
' en-GB: 28/05/2012 11:30:00
' ru: 28.05.2012 11:30:00
' fr: 28/05/2012 11:30:00
IFormattable ã¤ã³ã¿ã¼ãã§ã¤ã¹
éå¸¸ãæ¸å¼æå®æååããã³ ToString
ãã©ã¡ã¼ã¿ã¼ã使ç¨ã㦠IFormatProvider ã¡ã½ããããªã¼ãã¼ãã¼ãããåã¯ã IFormattable ã¤ã³ã¿ã¼ãã§ã¤ã¹ãå®è£
ãã¾ãã ãã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ã«ã¯ã IFormattable.ToString(String, IFormatProvider)ã¨ããåä¸ã®ã¡ã³ãã¼ãããã¾ãããã®ã¡ã³ãã¼ã«ã¯ããã©ã¡ã¼ã¿ã¼ã¨ãã¦æ¸å¼æå®æååã¨æ¸å¼ãããã¤ãã¼ã®ä¸¡æ¹ãå«ã¾ãã¦ãã¾ãã
ã¢ããªã±ã¼ã·ã§ã³å®ç¾©ã®ã¯ã©ã¹ã« IFormattable ã¤ã³ã¿ã¼ãã§ã¤ã¹ãå®è£ ããå ´åã2 ã¤ã®å©ç¹ãããã¾ãã
Convert ã¯ã©ã¹ã«ããæåå夿ããµãã¼ãããã¾ãã Convert.ToString(Object) ã¡ã½ããããã³ Convert.ToString(Object, IFormatProvider) ã¡ã½ãããå¼ã³åºãã¨ãèªåçã« IFormattable ã®å®è£ ãå¼ã³åºããã¾ãã
è¤åæ¸å¼æå®ããµãã¼ãããã¾ãã æ¸å¼æå®æååãå«ãæ¸å¼æå®é ç®ã使ç¨ãã¦ã«ã¹ã¿ã ã®åã®æ¸å¼ãè¨å®ããå ´åã«ãå ±éè¨èªã©ã³ã¿ã¤ã ã«ãã£ã¦èªåçã« IFormattable ã®å®è£ ãå¼ã³åºãããããã«æ¸å¼æå®æååãæ¸¡ããã¾ãã String.Format ã¡ã½ããã Console.WriteLineã¡ã½ããã使ç¨ããè¤åæ¸å¼æå®ã®è©³ç´°ã«ã¤ãã¦ã¯ãã è¤åæ¸å¼æå® ãã®ã»ã¯ã·ã§ã³ãåç §ãã¦ãã ããã
次ã®ä¾ã§ã¯ã Temperature
ã¤ã³ã¿ã¼ãã§ã¤ã¹ãå®è£
ãã IFormattable ã¯ã©ã¹ãå®ç¾©ãã¦ãã¾ãã ãã®ã¯ã©ã¹ã§ã¯ãæ¸©åº¦ãææ°ã§è¡¨ç¤ºããããã« "C" æ¸å¼æå®åã¾ã㯠"G" æ¸å¼æå®åãè¯æ°ã§è¡¨ç¤ºããããã« "F" æ¸å¼æå®åãã±ã«ãã³ã§è¡¨ç¤ºããããã« "K" æ¸å¼æå®åããããããµãã¼ããã¦ãã¾ãã
using System;
using System.Globalization;
namespace HotAndCold
{
public class Temperature : IFormattable
{
private decimal m_Temp;
public Temperature(decimal temperature)
{
this.m_Temp = temperature;
}
public decimal Celsius
{
get { return this.m_Temp; }
}
public decimal Kelvin
{
get { return this.m_Temp + 273.15m; }
}
public decimal Fahrenheit
{
get { return Math.Round((decimal)this.m_Temp * 9 / 5 + 32, 2); }
}
public override string ToString()
{
return this.ToString("G", null);
}
public string ToString(string format)
{
return this.ToString(format, null);
}
public string ToString(string format, IFormatProvider provider)
{
// Handle null or empty arguments.
if (String.IsNullOrEmpty(format))
format = "G";
// Remove any white space and covert to uppercase.
format = format.Trim().ToUpperInvariant();
if (provider == null)
provider = NumberFormatInfo.CurrentInfo;
switch (format)
{
// Convert temperature to Fahrenheit and return string.
case "F":
return this.Fahrenheit.ToString("N2", provider) + "°F";
// Convert temperature to Kelvin and return string.
case "K":
return this.Kelvin.ToString("N2", provider) + "K";
// Return temperature in Celsius.
case "C":
case "G":
return this.Celsius.ToString("N2", provider) + "°C";
default:
throw new FormatException(String.Format("The '{0}' format string is not supported.", format));
}
}
}
Public Class Temperature : Implements IFormattable
Private m_Temp As Decimal
Public Sub New(temperature As Decimal)
Me.m_Temp = temperature
End Sub
Public ReadOnly Property Celsius() As Decimal
Get
Return Me.m_Temp
End Get
End Property
Public ReadOnly Property Kelvin() As Decimal
Get
Return Me.m_Temp + 273.15D
End Get
End Property
Public ReadOnly Property Fahrenheit() As Decimal
Get
Return Math.Round(CDec(Me.m_Temp * 9 / 5 + 32), 2)
End Get
End Property
Public Overrides Function ToString() As String
Return Me.ToString("G", Nothing)
End Function
Public Overloads Function ToString(format As String) As String
Return Me.ToString(format, Nothing)
End Function
Public Overloads Function ToString(format As String, provider As IFormatProvider) As String _
Implements IFormattable.ToString
' Handle null or empty arguments.
If String.IsNullOrEmpty(format) Then format = "G"
' Remove any white space and convert to uppercase.
format = format.Trim().ToUpperInvariant()
If provider Is Nothing Then provider = NumberFormatInfo.CurrentInfo
Select Case format
' Convert temperature to Fahrenheit and return string.
Case "F"
Return Me.Fahrenheit.ToString("N2", provider) & "°F"
' Convert temperature to Kelvin and return string.
Case "K"
Return Me.Kelvin.ToString("N2", provider) & "K"
' Return temperature in Celsius.
Case "C", "G"
Return Me.Celsius.ToString("N2", provider) & "°C"
Case Else
Throw New FormatException(String.Format($"The '{format}' format string is not supported."))
End Select
End Function
End Class
次ã®ä¾ã§ã¯ã Temperature
ãªãã¸ã§ã¯ããã¤ã³ã¹ã¿ã³ã¹åãã¦ãã¾ãã ãã®å¾ã ToString ã¡ã½ãããå¼ã³åºããããã¤ãã®è¤åæ¸å¼æå®æååã使ç¨ã㦠Temperature
ãªãã¸ã§ã¯ãã®ãã¾ãã¾ãªæååå½¢å¼ãåå¾ãã¾ãã ããã«ããããã®åã¡ã½ããå¼ã³åºãã§ã IFormattable ã¯ã©ã¹ã® Temperature
å®è£
ãå¼ã³åºãã¦ãã¾ãã
public class Example11
{
public static void Main()
{
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
Temperature temp = new Temperature(22m);
Console.WriteLine(Convert.ToString(temp, new CultureInfo("ja-JP")));
Console.WriteLine($"Temperature: {temp:K}");
Console.WriteLine($"Temperature: {temp:F}");
Console.WriteLine(String.Format(new CultureInfo("fr-FR"), "Temperature: {0:F}", temp));
}
}
// The example displays the following output:
// 22.00°C
// Temperature: 295.15K
// Temperature: 71.60°F
// Temperature: 71,60°F
Public Module Example12
Public Sub Main12()
Dim temp As New Temperature(22D)
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US")
Console.WriteLine(Convert.ToString(temp, New CultureInfo("ja-JP")))
Console.WriteLine($"Temperature: {temp:K}")
Console.WriteLine($"Temperature: {temp:F}")
Console.WriteLine(String.Format(New CultureInfo("fr-FR"), $"Temperature: {temp:F}"))
End Sub
End Module
' The example displays the following output:
' 22.00°C
' Temperature: 295.15K
' Temperature: 71.60°F
' Temperature: 71,60°F
è¤åæ¸å¼æå®
String.FormatãStringBuilder.AppendFormatãªã©ã®ä¸é¨ã®ã¡ã½ããã§ã¯ãè¤åæ¸å¼ããµãã¼ãããã¦ãã¾ãã è¤åæ¸å¼æå®æååã¯ä¸ç¨®ã®ãã³ãã¬ã¼ãã§ã0 å以ä¸ã®ãªãã¸ã§ã¯ãã®æååå½¢å¼ãçµã¿è¾¼ã¾ããåä¸ã®æååãè¿ãã¾ãã åãªãã¸ã§ã¯ãã¯ãã¤ã³ããã¯ã¹ä»ãã®æ¸å¼æå®é
ç®ã«ãã£ã¦ãè¤åæ¸å¼æå®æååã§è¡¨ç¾ããã¾ãã æ¸å¼æå®é
ç®ã®ã¤ã³ããã¯ã¹ã¯ãããã表ããªãã¸ã§ã¯ãã®ã¡ã½ããã®ãã©ã¡ã¼ã¿ã¼ ãªã¹ãå
ã®ä½ç½®ã¨å¯¾å¿ãã¦ãã¾ãã ã¤ã³ããã¯ã¹ã¯ 0 ããå§ã¾ãã¾ãã ãã¨ãã°ã String.Format ã¡ã½ããã®æ¬¡ã®ã¡ã½ããå¼ã³åºãã§ã¯ãæåã®æ¸å¼æå®é
ç® {0:D}
㯠thatDate
ã®æååå½¢å¼ã«ã2 çªç®ã®æ¸å¼æå®é
ç® {1}
㯠item1
ã®æååå½¢å¼ã«ã3 çªç®ã®æ¸å¼æå®é
ç® {2:C2}
㯠item1.Value
ã®æååå½¢å¼ã«ç½®ãæãããã¾ãã
result = String.Format("On {0:d}, the inventory of {1} was worth {2:C2}.",
thatDate, item1, item1.Value);
Console.WriteLine(result);
// The example displays output like the following if run on a system
// whose current culture is en-US:
// On 5/1/2009, the inventory of WidgetA was worth $107.44.
result = String.Format("On {0:d}, the inventory of {1} was worth {2:C2}.",
thatDate, item1, item1.Value)
Console.WriteLine(result)
' The example displays output like the following if run on a system
' whose current culture is en-US:
' On 5/1/2009, the inventory of WidgetA was worth $107.44.
æ¸å¼é ç®ãããã«å¯¾å¿ãããªãã¸ã§ã¯ãã®æååå½¢å¼ã«ç½®æãããã¨ã«å ãã¦ãæ¸å¼é ç®ã¯ä»¥ä¸ãå¶å¾¡ãããã¨ãã§ãã¾ãã
ãªãã¸ã§ã¯ããæååã¨ãã¦è¡¨ç¾ããç¹å®ã®æ¹æ³ (ãªãã¸ã§ã¯ãã IFormattable ã¤ã³ã¿ã¼ãã§ã¤ã¹ãå®è£ ããæ¸å¼æååããµãã¼ãããå ´å)ã ããã¯ã
:
(ã³ãã³) ä»ãã®æ¸å¼é ç®ã®ã¤ã³ããã¯ã¹ã«ãæå¹ãªæ¸å¼æååãç¶ãããã¨ã«ããã¾ãã åã®ä¾ã§ã¯ã"d" (çãæ¥ä»ãã¿ã¼ã³) æ¸å¼æå®æåå (ãã¨ãã°ã{0:d}
) ã§æ¥ä»å¤ãæ¸å¼è¨å®ããæ°å¤ã "C2" æ¸å¼æå®æåå (ãã¨ãã°ã{2:C2}
) ã§æ¸å¼è¨å®ãã¦ã2 æ¡ã®å°æ°é¨ãæã¤é貨å¤ã¨ãã¦æ°å¤ã表ãã¾ãããªãã¸ã§ã¯ãã®æååå½¢å¼ãå«ããã£ã¼ã«ãã®å¹ ãããã³ãã®ãã£ã¼ã«ãã®æååå½¢å¼ã®é ç½®ã ããã¯ãæ¸å¼é ç®ã®ã¤ã³ããã¯ã¹ã«
,
ï¼ã³ã³ãï¼ãç¶ãããã®å¾ã«ãã£ã¼ã«ãã®å¹ ãæå®ãããã¨ã§è¡ãã¾ãã ãã£ã¼ã«ãã®å¹ ãæ£ã®å¤ã®å ´åãæååã¯ãã£ã¼ã«ãã§å³æãã«ãããã£ã¼ã«ãã®å¹ ãè² ã®å¤ã®å ´åãå·¦æãã«ãã¾ãã æ¬¡ã®ä¾ã§ã¯ã20 æåã®ãã£ã¼ã«ãã§æ¥ä»ã®å¤ãå·¦æãã«ãã11 æåã®ãã£ã¼ã«ãã§ã1 æ¡ã®å°æ°é¨ãå«ã 10 鲿°å¤ã峿ãã«ãã¾ããDateTime startDate = new DateTime(2015, 8, 28, 6, 0, 0); decimal[] temps = { 73.452m, 68.98m, 72.6m, 69.24563m, 74.1m, 72.156m, 72.228m }; Console.WriteLine("{0,-20} {1,11}\n", "Date", "Temperature"); for (int ctr = 0; ctr < temps.Length; ctr++) Console.WriteLine("{0,-20:g} {1,11:N1}", startDate.AddDays(ctr), temps[ctr]); // The example displays the following output: // Date Temperature // // 8/28/2015 6:00 AM 73.5 // 8/29/2015 6:00 AM 69.0 // 8/30/2015 6:00 AM 72.6 // 8/31/2015 6:00 AM 69.2 // 9/1/2015 6:00 AM 74.1 // 9/2/2015 6:00 AM 72.2 // 9/3/2015 6:00 AM 72.2
Dim startDate As New Date(2015, 8, 28, 6, 0, 0) Dim temps() As Decimal = {73.452, 68.98, 72.6, 69.24563, 74.1, 72.156, 72.228} Console.WriteLine("{0,-20} {1,11}", "Date", "Temperature") Console.WriteLine() For ctr As Integer = 0 To temps.Length - 1 Console.WriteLine("{0,-20:g} {1,11:N1}", startDate.AddDays(ctr), temps(ctr)) Next ' The example displays the following output: ' Date Temperature ' ' 8/28/2015 6:00 AM 73.5 ' 8/29/2015 6:00 AM 69.0 ' 8/30/2015 6:00 AM 72.6 ' 8/31/2015 6:00 AM 69.2 ' 9/1/2015 6:00 AM 74.1 ' 9/2/2015 6:00 AM 72.2 ' 9/3/2015 6:00 AM 72.2
é ç½®æååã³ã³ãã¼ãã³ãã¨æ¸å¼æå®æååã³ã³ãã¼ãã³ãã®ä¸¡æ¹ãåå¨ããå ´åãåè ã¯å¾è (ãã¨ãã°ã
{0,-20:g}
) ã®åã«ããã¾ãã
è¤åæ¸å¼æå®ã®è©³ç´°ã«ã¤ãã¦ã¯ãã è¤åæ¸å¼æå®ããåç §ãã¦ãã ããã
ICustomFormatter ã使ç¨ããã«ã¹ã¿ã æ¸å¼è¨å®
String.Format(IFormatProvider, String, Object[]) ããã³ StringBuilder.AppendFormat(IFormatProvider, String, Object[])ã® 2 ã¤ã®è¤åæ¸å¼æå®ã¡ã½ããã«ã¯ãã«ã¹ã¿ã æ¸å¼è¨å®ããµãã¼ããã¦ããæ¸å¼ãããã¤ãã¼ ãã©ã¡ã¼ã¿ã¼ãå«ã¾ãã¦ãã¾ãã ãããã®æ¸å¼æå®ã¡ã½ããã®ãããããå¼ã³åºãã¨ãæ¸å¼ãããã¤ãã¼ã® Type ã¡ã½ããã« ICustomFormatter ã¤ã³ã¿ã¼ãã§ã¤ã¹ã表ã GetFormat ãªãã¸ã§ã¯ããæ¸¡ããã¾ãã æ¬¡ã«ã GetFormat ã¡ã½ããã«ãã£ã¦ãã«ã¹ã¿ã æ¸å¼è¨å®ãæä¾ãã ICustomFormatter ã®å®è£ ãè¿ããã¾ãã
ICustomFormatter ã¤ã³ã¿ã¼ãã§ã¤ã¹ã«ã¯ã Format(String, Object, IFormatProvider)ã¨ããåä¸ã®ã¡ã½ãããããã¾ãããã®ã¡ã½ããã¯ãè¤åæ¸å¼æå®æååã®æ¸å¼æå®é
ç®ãã¨ã« 1 åãè¤åæ¸å¼æå®ã¡ã½ããã«ãã£ã¦èªåçã«å¼ã³åºããã¾ãã Format(String, Object, IFormatProvider) ã¡ã½ããã«ã¯ã3 ã¤ã®ãã©ã¡ã¼ã¿ã¼ãããã¾ããæ¸å¼æå®é
ç®ã® formatString
弿°ãè¡¨ãæ¸å¼æå®æååãæ¸å¼ãè¨å®ãããªãã¸ã§ã¯ããããã³æ¸å¼æå®ãµã¼ãã¹ãæä¾ãã IFormatProvider ãªãã¸ã§ã¯ãã® 3 ã¤ã§ãã é常ã¯ã ICustomFormatter ãå®è£
ããã¯ã©ã¹ã§ã¯ IFormatProviderãå®è£
ããããããã®æå¾ã®ãã©ã¡ã¼ã¿ã¼ã¯ã«ã¹ã¿ã æ¸å¼æå®ã¯ã©ã¹èªä½ã¸ã®åç
§ã«ãªãã¾ãã ãã®ã¡ã½ããã¯ãæ¸å¼ãè¨å®ãããªãã¸ã§ã¯ãã®ã«ã¹ã¿ã æ¸å¼ã®æååå½¢å¼ãè¿ãã¾ãã ãªãã¸ã§ã¯ãã®æ¸å¼ãè¨å®ã§ããªãå ´åã¯ãnull åç
§ (Visual Basic ã®å ´åã¯Nothing
) ãè¿ãã¾ãã
æ´æ°å¤ã 2 æ¡ã® 16 é²å¤ã¨ããã«ç¶ã 1 ã¤ã®ç©ºç½ã®ã·ã¼ã±ã³ã¹ã¨ãã¦è¡¨ç¤ºããã ICustomFormatter ã¨ããååã® ByteByByteFormatter
ã®å®è£
ã®ä¾ã次ã«ç¤ºãã¾ãã
public class ByteByByteFormatter : IFormatProvider, ICustomFormatter
{
public object? GetFormat(Type? formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}
public string Format(string? format, object? arg,
IFormatProvider? formatProvider)
{
if ((formatProvider is not null) && !formatProvider.Equals(this)) return "";
// Handle only hexadecimal format string.
if ((format is not null) && !format.StartsWith("X")) return "";
byte[] bytes;
// Handle only integral types.
if (arg is Int16)
bytes = BitConverter.GetBytes((Int16)arg);
else if (arg is Int32)
bytes = BitConverter.GetBytes((Int32)arg);
else if (arg is Int64)
bytes = BitConverter.GetBytes((Int64)arg);
else if (arg is UInt16)
bytes = BitConverter.GetBytes((UInt16)arg);
else if (arg is UInt32)
bytes = BitConverter.GetBytes((UInt32)arg);
else if (arg is UInt64)
bytes = BitConverter.GetBytes((UInt64)arg);
else
return "";
string output= "";
for (int ctr = bytes.Length - 1; ctr >= 0; ctr--)
output += string.Format("{0:X2} ", bytes[ctr]);
return output.Trim();
}
}
Public Class ByteByByteFormatter : Implements IFormatProvider, ICustomFormatter
Public Function GetFormat(formatType As Type) As Object _
Implements IFormatProvider.GetFormat
If formatType Is GetType(ICustomFormatter) Then
Return Me
Else
Return Nothing
End If
End Function
Public Function Format(fmt As String, arg As Object,
formatProvider As IFormatProvider) As String _
Implements ICustomFormatter.Format
If Not formatProvider.Equals(Me) Then Return Nothing
' Handle only hexadecimal format string.
If Not fmt.StartsWith("X") Then
Return Nothing
End If
' Handle only integral types.
If Not typeof arg Is Byte AndAlso
Not typeof arg Is Int16 AndAlso
Not typeof arg Is Int32 AndAlso
Not typeof arg Is Int64 AndAlso
Not typeof arg Is SByte AndAlso
Not typeof arg Is UInt16 AndAlso
Not typeof arg Is UInt32 AndAlso
Not typeof arg Is UInt64 Then _
Return Nothing
Dim bytes() As Byte = BitConverter.GetBytes(arg)
Dim output As String = Nothing
For ctr As Integer = bytes.Length - 1 To 0 Step -1
output += String.Format("{0:X2} ", bytes(ctr))
Next
Return output.Trim()
End Function
End Class
ByteByByteFormatter
ã¯ã©ã¹ã使ç¨ãã¦æ´æ°å¤ã®æ¸å¼ãè¨å®ããä¾ã次ã«ç¤ºãã¾ãã ICustomFormatter.Format ã¡ã½ããã2åç®ã® String.Format(IFormatProvider, String, Object[]) ã¡ã½ããå¼ã³åºãã§è¤æ°åå¼ã³åºããããã¨ã«æ³¨æãã¦ãã ãããã¾ãã3åç®ã®ã¡ã½ããå¼ã³åºãã§ã¯ãNumberFormatInfo ã®ããã©ã«ããããã¤ãã¼ã使ç¨ããã¾ããByteByByteFormatter.Format
ã¡ã½ãã㯠"N0" ãã©ã¼ãããæååãèªèãããnull åç
§ (Nothing
ã® Visual Basic) ãè¿ãã¾ãã
public class Example10
{
public static void Main()
{
long value = 3210662321;
byte value1 = 214;
byte value2 = 19;
Console.WriteLine(string.Format(new ByteByByteFormatter(), "{0:X}", value));
Console.WriteLine(string.Format(new ByteByByteFormatter(), "{0:X} And {1:X} = {2:X} ({2:000})",
value1, value2, value1 & value2));
Console.WriteLine(string.Format(new ByteByByteFormatter(), "{0,10:N0}", value));
}
}
// The example displays the following output:
// 00 00 00 00 BF 5E D1 B1
// 00 D6 And 00 13 = 00 12 (018)
// 3,210,662,321
Public Module Example10
Public Sub Main10()
Dim value As Long = 3210662321
Dim value1 As Byte = 214
Dim value2 As Byte = 19
Console.WriteLine((String.Format(New ByteByByteFormatter(), "{0:X}", value)))
Console.WriteLine((String.Format(New ByteByByteFormatter(), "{0:X} And {1:X} = {2:X} ({2:000})",
value1, value2, value1 And value2)))
Console.WriteLine(String.Format(New ByteByByteFormatter(), "{0,10:N0}", value))
End Sub
End Module
' The example displays the following output:
' 00 00 00 00 BF 5E D1 B1
' 00 D6 And 00 13 = 00 12 (018)
' 3,210,662,321
é¢é£é ç®
ã¿ã¤ãã« | å®ç¾© |
---|---|
æ¨æºã®æ°å¤æ¸å¼æå®æåå | æ°å¤ã®ä¸è¬çãªæåå表ç¾ãçæããæ¨æºæ¸å¼æååã«ã¤ãã¦èª¬æãã¾ãã |
ã«ã¹ã¿ã æ°å¤å½¢å¼æåå | æ°å¤ã«å¯¾ããã¢ããªã±ã¼ã·ã§ã³åºæã®æååå½¢å¼ã使ããã«ã¹ã¿ã æ¸å¼æå®æååã«ã¤ãã¦èª¬æãã¾ãã |
æ¨æºã®æ¥æå½¢å¼æåå | DateTime å¤ã«å¯¾ãã¦ä¸è¬çã«ä½¿ç¨ãããæååå½¢å¼ã使ããæ¨æºæ¸å¼æå®æååã«ã¤ãã¦èª¬æãã¾ãã |
ã«ã¹ã¿ã æ¥æå½¢å¼æåå | DateTime å¤ã«å¯¾ããã¢ããªã±ã¼ã·ã§ã³åºæã®æååå½¢å¼ã使ããã«ã¹ã¿ã æ¸å¼æå®æååã«ã¤ãã¦èª¬æãã¾ãã |
æ¨æºã®æéé鿏弿宿åå | æéééã«å¯¾ãã¦ä¸è¬çã«ä½¿ç¨ãããæååå½¢å¼ã使ããæ¨æºæ¸å¼æå®æååã«ã¤ãã¦èª¬æãã¾ãã |
ã«ã¹ã¿ã æéé鿏弿宿åå | æéééã«å¯¾ããã¢ããªã±ã¼ã·ã§ã³åºæã®æååå½¢å¼ã使ããã«ã¹ã¿ã æ¸å¼æå®æååã«ã¤ãã¦èª¬æãã¾ãã |
åæåæ¸å¼æå®æåå | åæåã®å¤ã®æåå表ç¾ã使ããããã«ä½¿ç¨ãããæ¨æºãã©ã¼ãããæååã«ã¤ãã¦èª¬æãã¾ãã |
è¤åæ¸å¼æå® | æååã« 1 ã¤ä»¥ä¸ã®æ¸å¼æå®ãããå¤ãåãè¾¼ãæ¹æ³ã«ã¤ãã¦èª¬æãã¾ãã ãã®æååã¯ãã³ã³ã½ã¼ã«ã«è¡¨ç¤ºããããã¹ããªã¼ã ã«æ¸ãè¾¼ãã ãã§ãã¾ãã |
æååã®è§£æ | ãªãã¸ã§ã¯ãã®æåå表ç¾ã«ãã£ã¦æå®ãããå¤ã«ãªãã¸ã§ã¯ããåæåããæ¹æ³ã«ã¤ãã¦èª¬æãã¾ãã è§£æã¯æ¸å¼è¨å®ã®éã®æä½ã§ãã |