データ型 | メモ |
---|---|
decimal | 128ビットの数値型。リテラルには最後にmを付ける(10.10mなど)。丸め誤差がない。 |
char | 16ビットの符号なし整数型。Unicode文字を表す。 |
構造体 | struct 構造体名 { } |
列挙型 | enum 列挙型名 { 列挙値のリスト }; |
参照型 | メモ |
---|---|
クラス | class クラス名 { } |
インターフェイス | public interface インターフェイス名 { } |
フォーマット文字列 | メモ |
---|---|
Console.WriteLine("{0:#.##}", 10.0/3.0); | 出力:3.33 |
Console.WriteLine("{0:C}", 12345m); | 出力:¥12,345 |
文字列を数値型データに変換する
string str;
try {
str = Console.ReadLine();
decimal decimalVal = Decimal.Parse(str);
str = Console.ReadLine();
double doubleVal = Double.Parse(str);
str = Console.ReadLine();
float floatVal = Single.Parse(str);
str = Console.ReadLine();
long longVal = Int64.Parse(str);
str = Console.ReadLine();
int intVal = Int32.Parse(str);
str = Console.ReadLine();
short shortVal = Int16.Parse(str);
str = Console.ReadLine();
ulong ulongVal = UInt64.Parse(str);
str = Console.ReadLine();
uint uintVal = UInt32.Parse(str);
str = Console.ReadLine();
ushort ushortVal = UInt16.Parse(str);
str = Console.ReadLine();
byte byteVal = Byte.Parse(str);
str = Console.ReadLine();
sbyte sbyteVal = SByte.Parse(str);
}
catch (FormatException exc) {
}
catch (OverfloatException exc) {
}