블로그 이미지
Magic_kit
study 관련자료를 한곳으로 자기 개발 목적으로 재태크 재무 관리 목적으로 일상생활의 팁을 공유 하기 위하여 블로그를 개설 하였습니다.

calendar

1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28

Category

Recent Post

Recent Comment

Archive

'.Net Project/.Net C#'에 해당되는 글 69

  1. 2009.08.10 28장 Round (반올림)
  2. 2009.08.10 27장 Math(수학관련함수)
  3. 2009.08.10 26장 파일명추출
  4. 2009.08.10 25장 String Format
2009. 8. 10. 18:47 .Net Project/.Net C#
반응형

using System;

public class 반올림
{
    public static void Main(string[] args)
    {
        double data = 1234.5678;
        Console.WriteLine(Math.Round(data, 2));
        Console.WriteLine(MyRound(data, 3));

      // 반올림할 자리수에 5를 더하고 정수를 곱해 소수 자리를 자른 후
      // 다시 실수를 곱하여 실수로 만들어 준다.
        // double temp = (int)((data + 0.5) * 1) / 1.0;
        // double temp = (int)((data + 0.05) * 10) / 10.0;
        double temp = (int)((data + 0.005) * 100) / 100.0;
        Console.WriteLine("{0}", temp);
    }

    /// <summary>
    /// 반올림 함수
    /// </summary>
    /// <param name="num">실수형</param>
    /// <param name="pos">자리수</param>
    /// <returns></returns>
    public static double MyRound(double num, int pos)
    {
        double result = 0.0;
        double half = 0.5;
        double factor = 1;

        for (int i = 0; i < pos; i++)
        {
            half *= 0.1;
            factor *= 10;
        }
        result = (int)((num + half) * factor) / (double)factor;
        return result;
    }
}

- Math.Round
http://msdn.microsoft.com/ko-kr/library/system.math_members(VS.95).aspx

반응형

'.Net Project > .Net C#' 카테고리의 다른 글

30장 Random Class(랜덤 클래스)  (0) 2009.08.10
29장 환경변수  (0) 2009.08.10
27장 Math(수학관련함수)  (0) 2009.08.10
26장 파일명추출  (0) 2009.08.10
25장 String Format  (0) 2009.08.10
posted by Magic_kit
2009. 8. 10. 18:46 .Net Project/.Net C#
반응형

using System;

public class Math관련
{
    public static void Main(string[] args)
    {
        Console.WriteLine(Math.E);                                // 자연로그
        Console.WriteLine(Math.PI);                               // 원주율

        Console.WriteLine(Math.Abs(-10));                     // 절대값
        Console.WriteLine(Math.Pow(2, 10));                  // 제곱근
        Console.WriteLine(Math.Round(1234.5678, 2));     // 자리수 이하 반올림
        Console.WriteLine(Math.Max(3, 5));                   // 최대값
        Console.WriteLine(Math.Min(3, 5));                    // 최소값
    }
}

- Math
http://msdn.microsoft.com/ko-kr/library/system.math_members(VS.95).aspx

반응형

'.Net Project > .Net C#' 카테고리의 다른 글

29장 환경변수  (0) 2009.08.10
28장 Round (반올림)  (0) 2009.08.10
26장 파일명추출  (0) 2009.08.10
25장 String Format  (0) 2009.08.10
24장 String Class(스트링 클래스)  (0) 2009.08.10
posted by Magic_kit
2009. 8. 10. 18:45 .Net Project/.Net C#
반응형

using System;

public class 파일명추출
{
    public static void Main(string[] args)
    {
        string dir = "C:\\Website\\RedPlus\\images\\test.gif";
        string fullName = string.Empty;    // 초기화
        string name = "";
        string ext = "";

        // 파일명 : test.gif
       // 가장뒤의 "\\"의 뒤부터 출력

        fullName = dir.Substring(dir.LastIndexOf("\\") + 1);
         // 파일명에서 0번 index 부터 "."의 index까지 출력
         // 확장자 : gif
         name = fullName.Substring(0, fullName.LastIndexOf(".")); 
      
         // 파일명에서 "." index 이후 출력
         ext = fullName.Substring(fullName.LastIndexOf(".") + 1);  
        Console.WriteLine("파일명 : {0}", fullName);
        Console.WriteLine("순수 파일명 : {0}", name);
        Console.WriteLine("확장자 : {0}", ext);
    }
}

반응형

'.Net Project > .Net C#' 카테고리의 다른 글

28장 Round (반올림)  (0) 2009.08.10
27장 Math(수학관련함수)  (0) 2009.08.10
25장 String Format  (0) 2009.08.10
24장 String Class(스트링 클래스)  (0) 2009.08.10
23장 알고리즘(간단수열)  (0) 2009.08.10
posted by Magic_kit
2009. 8. 10. 18:43 .Net Project/.Net C#
반응형

using System;

public class StringFormat
{
    public static void Main(string[] args)
    {
        int i = 1234;
        double j = 1234.5678;
        string k = "1234";

        // 문자열로 연결
        string result = string.Format("{0} {1} {2}", i, j, k);
        Console.WriteLine("{0}", result);

        // 정수 또는 실수형 자리수 표현 가능
        // webform, winform의 경우 Console.WriteLine에서 지원 하지 못함
        // 따라서 string.Format을 사용 하여야 함
        result = string.Format("{0:###,###}", i);
        Console.WriteLine(result);
       
        // 1,234.57 (형식밖의 수는 반올림)
        Console.WriteLine(string.Format("{0:###,###.##}", j));
       
        // 1,234.5700 (0 = 해당자리에 값이 없을 경우 0으로 표시)
        Console.WriteLine(string.Format("{0:###,###.##0000}", j));
       
       // 1,235
        Console.WriteLine(string.Format("{0:000,###}", j));
       
        // string 형태의 숫자는 format 적용 불가
        Console.WriteLine(string.Format("{0:000,###}", k));
       
        // string 형태의 숫자를 format 적용 하기 위해서는
        Console.WriteLine(string.Format("{0:000,###}", Convert.ToInt32(k)));

        // 긴 문자열 연결시 (가장 널리 쓰이는 방식)
        sult = string.Format("{0}\n{1}\n{2}",
            "<script type='text.css'>",
            string.Format("window.alert(\"{0}\")", "안녕하세요"),
            "</script>");
        Console.WriteLine(result);

        // @"~" 내용을 그대로 입력/
        result = @"
            <script type='text/javascript'>
            windows.alert('반갑습니다.');
            </script>
        ";
        Console.WriteLine(result);

        // + 연산자
        result = "<script type=text/janvscript>\n"
            + "windows.alert('반갑습니다.');\n"
            + "</script>";
        Console.WriteLine(result);

        // 채우기
        string data = "1234";
        Console.WriteLine("{0}", data.PadLeft(10,'#'));     // ######1234
        Console.WriteLine("{0}", data.PadRight(10, '?'));   // 1234??????
    }
}

- string.format
http://msdn.microsoft.com/ko-kr/library/system.string.format(VS.95).aspx


- ConvertToint32
http://msdn.microsoft.com/ko-kr/library/system.convert.toint32(VS.95).aspx


- string.PadLeft & PadRight
http://msdn.microsoft.com/ko-kr/library/system.string.padleft(VS.95).aspx
http://msdn.microsoft.com/ko-kr/library/system.string.padright(VS.95).aspx

반응형

'.Net Project > .Net C#' 카테고리의 다른 글

27장 Math(수학관련함수)  (0) 2009.08.10
26장 파일명추출  (0) 2009.08.10
24장 String Class(스트링 클래스)  (0) 2009.08.10
23장 알고리즘(간단수열)  (0) 2009.08.10
22장 알고리즘(간단수열)  (0) 2009.08.10
posted by Magic_kit