첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ Math 클래스 : IEEE 754 실수 계산하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


RoundType.cs

 

 

namespace TestProject

{

    /// <summary>

    /// 반올림 타입

    /// </summary>

    public enum RoundType

    {

        /// <summary>

        /// 올림

        /// </summary>

        Ceiling,

 

        /// <summary>

        /// 반올림

        /// </summary>

        Round,

 

        /// <summary>

        /// 내림

        /// </summary>

        Truncate

    }

}

 

 

DoubleHelper.cs

 

 

using System;

 

namespace TestProject

{

    /// <summary>

    /// 배정도 실수 헬퍼

    /// </summary>

    public static class DoubleHelper

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region 반올림하기 - Round(value, roundDigit, rountType)

 

        /// <summary>

        /// 반올림하기

        /// </summary>

        /// <param name="value"></param>

        /// <param name="roundDigit">반올림 자리수</param>

        /// <param name="rountType">반올림 타입</param>

        /// <returns>반올림 결과</returns>

        public static double Round(double value, int roundDigit = 3, RoundType rountType = RoundType.Truncate)

        {

            double temporaryValue = Math.Pow(10, roundDigit) / 10;

 

            double resultValue = Math.Truncate(value * temporaryValue) / temporaryValue;

 

            switch(rountType)

            {

                case RoundType.Ceiling  : resultValue = Math.Ceiling (value * temporaryValue) / temporaryValue; break;

                case RoundType.Round    : resultValue = Math.Round   (value * temporaryValue) / temporaryValue; break;

                case RoundType.Truncate : resultValue = Math.Truncate(value * temporaryValue) / temporaryValue; break;

            }

 

            return resultValue;

        }

 

        #endregion

 

        #region 더하기 - Add(value1, value2, roundDigit, roundType)

 

        /// <summary>

        /// 더하기

        /// </summary>

        /// <param name="value1">값 1</param>

        /// <param name="value2">값 2</param>

        /// <param name="roundDigit">반올림 자리수</param>

        /// <param name="roundType">반올림 타입</param>

        /// <returns>더하기 결과</returns>

        public static double Add(double value1, double value2, int roundDigit = 3, RoundType roundType = RoundType.Truncate)

        {

            double resultValue = Convert.ToDouble(Convert.ToDecimal(value1) + Convert.ToDecimal(value2));

 

            return Round(resultValue, roundDigit, roundType);

        }

 

        #endregion

        #region  빼기 - Subtract(value1, value2, roundDigit, roundType)

 

        /// <summary>

        /// 빼기

        /// </summary>

        /// <param name="value1">값 1</param>

        /// <param name="value2">값 2</param>

        /// <param name="roundDigit">반올림 자리수</param>

        /// <param name="roundType">반올림 타입</param>

        /// <returns>빼기 결과</returns>

        public static double Subtract(double value1, double value2, int roundDigit = 3, RoundType roundType = RoundType.Truncate)

        {

            double resultValue = Convert.ToDouble(Convert.ToDecimal(value1) - Convert.ToDecimal(value2));

 

            return Round(resultValue, roundDigit, roundType);

        }

 

        #endregion

        #region 곱하기 - Multiply(value1, value2, roundDigit, roundType)

 

        /// <summary>

        /// 곱하기

        /// </summary>

        /// <param name="value1">값 1</param>

        /// <param name="value2">값 2</param>

        /// <param name="roundDigit">반올림 자리수</param>

        /// <param name="roundType">반올림 타입</param>

        /// <returns>곱하기 결과</returns>

        public static double Multiply(double value1, double value2, int roundDigit = 3, RoundType roundType = RoundType.Truncate)

        {

            double resultValue = Convert.ToDouble(Convert.ToDecimal(value1) * Convert.ToDecimal(value2));

 

            return Round(resultValue, roundDigit, roundType);

        }

 

        #endregion

        #region 나누기 - Divide(value1, value2, roundDigit, roundType)

 

        /// <summary>

        /// 2개 실수 나누기

        /// </summary>

        /// <param name="value1">값 1</param>

        /// <param name="value2">값 2</param>

        /// <param name="roundDigit">반올림 자리수</param>

        /// <param name="roundType">반올림 타입</param>

        /// <returns>나누기 결과</returns>

        public static double Divide(double value1, double value2, int roundDigit = 3, RoundType roundType = RoundType.Truncate)

        {

            double resultValue = Convert.ToDouble(Convert.ToDecimal(value1) / Convert.ToDecimal(value2));

 

            return Round(resultValue, roundDigit, roundType);

        }

 

        #endregion

 

        #region 더하기 - AddToInteger(value1, value2)

 

        /// <summary>

        /// 더하기

        /// </summary>

        /// <param name="value1">값 1</param>

        /// <param name="value2">값 2</param>

        /// <returns>더하기 결과</returns>

        public static int AddToInteger(double value1, double value2)

        {

            return Convert.ToInt32(Convert.ToDecimal(value1) + Convert.ToDecimal(value2));

        }

 

        #endregion

        #region 빼기 - SubtractToInteger(value1, value2)

 

        /// <summary>

        /// 빼기

        /// </summary>

        /// <param name="value1">값 1</param>

        /// <param name="value2">값 2</param>

        /// <returns>빼기 결과</returns>

        public static int SubtractToInteger(double value1, double value2)

        {

            return Convert.ToInt32(Convert.ToDecimal(value1) - Convert.ToDecimal(value2));

        }

 

        #endregion

        #region 곱하기 - MultiplyToInteger(value1, value2)

 

        /// <summary>

        /// 곱하기

        /// </summary>

        /// <param name="value1">값 1</param>

        /// <param name="value2">값 2</param>

        /// <returns>곱하기 결과</returns>

        public static int MultiplyToInteger(double value1, double value2)

        {

            return Convert.ToInt32(Convert.ToDecimal(value1) * Convert.ToDecimal(value2));

        }

 

        #endregion

        #region 나누기 - DivideToInteger(value1, value2)

 

        /// <summary>

        /// 나누기

        /// </summary>

        /// <param name="value1">값 1</param>

        /// <param name="value2">값 2</param>

        /// <returns>나누기 결과</returns>

        public static int DivideToInteger(double value1, double value2)

        {

            return Convert.ToInt32(Convert.ToDecimal(value1) / Convert.ToDecimal(value2));

        }

 

        #endregion

    }

}

 

 

Program.cs

 

 

using System;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 시작하기 - Main(argumentArray)

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        /// <param name="argumentArray">인자 배열</param>

        private static void Main(string[] argumentArray)

        {

            Console.Title = "Math 클래스 : IEEE 754 실수 계산하기";

 

            double value1 = 5.43;

            double value2 = 2.31;

 

            Console.WriteLine($"값 1 : {value1}");

            Console.WriteLine($"값 2 : {value2}");

 

            Console.WriteLine();

 

            Console.WriteLine($"값1 + 값2 = {DoubleHelper.Add     (value1, value2, 3, RoundType.Round)}");

            Console.WriteLine($"값1 - 값2 = {DoubleHelper.Subtract(value1, value2, 3, RoundType.Round)}");

            Console.WriteLine($"값1 * 값2 = {DoubleHelper.Multiply(value1, value2, 3, RoundType.Round)}");

            Console.WriteLine($"값1 / 값2 = {DoubleHelper.Divide  (value1, value2, 3, RoundType.Round)}");

 

            Console.WriteLine();

 

            Console.WriteLine($"값1 + 값2 = {DoubleHelper.AddToInteger     (value1, value2)}");

            Console.WriteLine($"값1 - 값2 = {DoubleHelper.SubtractToInteger(value1, value2)}");

            Console.WriteLine($"값1 * 값2 = {DoubleHelper.MultiplyToInteger(value1, value2)}");

            Console.WriteLine($"값1 / 값2 = {DoubleHelper.DivideToInteger  (value1, value2)}");

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요