728x90
<๋ฌธ์์ด์ ์ซ์๋ก> Parse!
C#์ ์ ์๊ณ์ด ํ์, ๋ถ๋ ์์์ ํ์ ๋ชจ๋์๊ฒ Parse()๋ผ๋ ๋ฉ์๋๋ฅผ ๋ฃ์๋ค.
์ด ๋ฉ์๋์ ์ซ์๋ก ๋ณํํ ๋ฌธ์์ด์ ๋๊ธฐ๋ฉด ์ซ์๋ก ๋ณํํด์ค๋ค.
using System;
using System.Text;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
string numbers = "12345";
string fnumbers = "123.45";
int a = int.Parse(numbers);
float b = float.Parse(fnumbers);
WriteLine(numbers); //12345
WriteLine(fnumbers); //123.45
WriteLine(a); //12345
WriteLine(b);//123.45
}
}
}
<์ซ์ ๋ฐ์ดํฐ ํ์์ ๋ฌธ์์ด๋ก>
์ ์ ๊ณ์ด ๋ฐ์ดํฐ ํ์์ด๋ ๋ถ๋ ์์์ ๋ฐ์ดํฐ ํ์์ ์์ ์ด ๊ฐ์ง ์ซ์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํ๋๋ก,
object๋ก๋ถํฐ ๋ฌผ๋ ค๋ฐ์ ToString() ๋ฉ์๋๋ฅผ ์ฌ์ ์(์ค๋ฒ๋ผ์ด๋)ํ๋ค.
์ซ์ ํ์ ๋ณ์์ ToString() ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ๊ฒ๋ง์ผ๋ก ์ซ์๋ก๋ถํฐ ๋ฌธ์์ด์ ์ป์ด๋ผ ์ ์๋ค.
using System;
using System.Text;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
string numbers = "12345";
string fnumbers = "123.45";
int a = int.Parse(numbers);
float b = float.Parse(fnumbers);
int c = 56789;
string d = c.ToString();
float e = 567.89f;
string f = e.ToString();
WriteLine(numbers); //12345
WriteLine(fnumbers); //123.45
WriteLine(a); //12345
WriteLine(b); //123.45
WriteLine(c); //56789
WriteLine(d); //56789
WriteLine(e); //567.89
WriteLine(f); //567.89
}
}
}
์ต์ข ํ ์คํธ ์์ค ์ฝ๋
using System;
using System.Text;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
string numbers = "12345";
string fnumbers = "123.45";
int a = int.Parse(numbers);
float b = float.Parse(fnumbers);
int c = 56789;
string d = c.ToString();
float e = 567.89f;
string f = e.ToString();
string g = "9999";
int h = Convert.ToInt32(g);
WriteLine(numbers); //12345
WriteLine(fnumbers); //123.45
WriteLine(a); //12345
WriteLine(b); //123.45
WriteLine(c); //56789
WriteLine(d); //56789
WriteLine(e); //567.89
WriteLine(f); //567.89
WriteLine(g); //9999
WriteLine(h); //9999
}
}
}
728x90
'C# > ์ด๊ฒ์ด C#์ด๋ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
7. Nullable, HasValue, Value (0) | 2022.02.07 |
---|---|
6. ์์์ ์ด๊ฑฐ ํ์ (const, enum): ํ๋ก๊ทธ๋๋จธ์ ์ค์๋ฅผ ๋ง๋ ์ฅ์น (0) | 2022.02.07 |
4. object ํ์, ๋ฐ์ฑ๊ณผ ์ธ๋ฐ์ฑ (0) | 2022.02.07 |
3. ๋ถํธ๊ฐ ์๋ ์ ์, ๋ถํธ๊ฐ ์๋ ์ ์, 2์ ๋ณด์๋ฒ (0) | 2022.02.06 |
3. object, ๋ฐ์ฑ๊ณผ ์ธ๋ฐ์ฑ, var (0) | 2022.02.04 |