Microsoft MVP성태의 닷넷 이야기
.NET Framework: 419. C# - BigDecimal [링크 복사], [링크+제목 복사],
조회: 32071
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

C# - BigDecimal

다음과 같은 질문이 있었습니다. ^^

BigDecimal 라이브러리
; http://social.msdn.microsoft.com/Forums/ko-KR/75cb5db0-6b78-4dc0-bd35-9f44f8c9ff9e/bigdecimal-?forum=dotnetko

BigDecimal의 대용으로 C#의 BigInteger가 있다는 답변이 뒤따르는데요. 사실 BigInteger와 BigDecimal은 소수점 처리 여부 자체에서 다르다는 특징이 있습니다. 이것은 int와 decimal의 차이에서 직관적인 유추가 가능합니다.

일례로, 자바로 다음과 같이 코드를 만들면 출력 결과가 10.5가 나오지만,

import java.math.BigDecimal;

public class testC 
{
    public static void main(String [] args)
    {
        BigDecimal bd = new BigDecimal(10.5);
        System.out.println(bd); // 출력: 10.5
    }
}

C#의 BigInteger로 동일하게 소스코드를 구성하면 출력 결과가 10이 나옵니다.

using System;
using System.Numerics;

class Program
{
    static void Main(string[] args)
    {
        BigInteger bi = new BigInteger(10.5);
        Console.WriteLine(bi); // 출력: 10
    }
}

그래서 나름대로 ^^ BigDecimal 구현체를 검색해서 추천한 것입니다.

BigDecimal.cs 
; https://gist.github.com/nberardi/2667136

실제로 위의 BigDecimal을 사용하면 소수점 보존을 해줍니다.

BigDecimal bd = new BigDecimal(10.5);
Console.WriteLine(bd.ToString()); // 출력: 10.5

BigDecimal 구현을 내부적으로 BigInteger로 이용했다고 해서 동일하다고 보면 안되겠지요. ^^

물론, "BigDecimal.cs"에 있는 소스코드가 자바의 BigDecimal의 기능을 모두 구현하지는 않았을 것입니다.

선택적으로는 다음고 같은 것들이 있군요.

dotnet-big-decimal 
; http://code.google.com/p/dotnet-big-decimal/source/browse/BigDecimal/BigDecimal.cs

W3b.Sine
; http://sine.codeplex.com/SourceControl/latest#W3b.Sine/W3b.Sine/Numbers/BigNum.cs

A BigFloat Library in C#
; https://www.codeproject.com/Articles/5375327/A-BigFloat-Library-in-Csharp

재미있는 글이 하나 더 있습니다.

BigInteger/BigDecimal
; http://geekswithblogs.net/gyoung/archive/2006/05/01/76869.aspx

위의 글에서는 .NET 1.1에 있었던 vjslib.dll을 참조 추가해서 사용하는 것을 언급하고 있는데요. 실제로 .NET 1.1이 설치된 "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322" 폴더에서 vjslib.dll을 가져와 참조 추가하고 vjsnativ.dll 파일을 exe와 같은 폴더에 복사해 주니 다음과 같이 사용할 수 있었습니다.

using System;
using java.math;

class Program
{
    static void Main(string[] args)
    {
        BigDecimal bd = new BigDecimal(10.5);
        Console.WriteLine(bd); // 출력 결과: 10.5000000000000000000000000000000000000000000000000
    }
}

문제가 있다면 .NET 1.1은 64비트 버전이 없기 때문에 x64 용 EXE에서는 사용할 수 없습니다. ^^

첨부 파일은 위의 java.math와 "BigDecimal.cs"에 대한 예제 코드를 포함하고 있습니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 2/28/2024]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 91  92  93  94  95  96  97  98  99  100  101  102  103  [104]  105  ...
NoWriterDateCnt.TitleFile(s)
11366정성태11/25/201720801오류 유형: 430. 이벤트 로그 - Cryptographic Services failed while processing the OnIdentity() call in the System Writer Object.
11365정성태11/25/201722126오류 유형: 429. 이벤트 로그 - User Policy could not be updated successfully
11364정성태11/24/201724794사물인터넷: 11. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스로 쓰는 방법 (절대 좌표) [2]
11363정성태11/23/201724743사물인터넷: 10. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스 + 키보드로 쓰는 방법 (두 번째 이야기)
11362정성태11/22/201720427오류 유형: 428. 윈도우 업데이트 KB4048953 - 0x800705b4 [2]
11361정성태11/22/201723414오류 유형: 427. 이벤트 로그 - Filter Manager failed to attach to volume '\Device\HarddiskVolume??' 0xC03A001C
11360정성태11/22/201723892오류 유형: 426. 이벤트 로그 - The kernel power manager has initiated a shutdown transition.
11359정성태11/16/201723138오류 유형: 425. 윈도우 10 Version 1709 (OS Build 16299.64) 업그레이드 시 발생한 문제 2가지
11358정성태11/15/201728153사물인터넷: 9. Visual Studio 2017에서 Raspberry Pi C++ 응용 프로그램 제작 [1]
11357정성태11/15/201728627개발 환경 구성: 336. 윈도우 10 Bash 쉘에서 C++ 컴파일하는 방법
11356정성태11/15/201730144사물인터넷: 8. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스 + 키보드로 쓰는 방법 [4]
11355정성태11/15/201725331사물인터넷: 7. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스로 쓰는 방법 [2]파일 다운로드2
11354정성태11/14/201730173사물인터넷: 6. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 키보드로 쓰는 방법 [8]
11353정성태11/14/201727160사물인터넷: 5. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 이더넷 카드로 쓰는 방법 [1]
11352정성태11/14/201723514사물인터넷: 4. Samba를 이용해 윈도우와 Raspberry Pi간의 파일 교환 [1]
11351정성태11/7/201726394.NET Framework: 698. C# 컴파일러 대신 직접 구현하는 비동기(async/await) 코드 [6]파일 다운로드1
11350정성태11/1/201722450디버깅 기술: 108. windbg 분석 사례 - Redis 서버로의 호출을 기다리면서 hang 현상 발생
11349정성태10/31/201723140디버깅 기술: 107. windbg - x64 SOS 확장의 !clrstack 명령어가 출력하는 Child SP 값의 의미 [1]파일 다운로드1
11348정성태10/31/201719349디버깅 기술: 106. windbg - x64 역어셈블 코드에서 닷넷 메서드 호출의 인자를 확인하는 방법
11347정성태10/28/201723114오류 유형: 424. Visual Studio - "클래스 다이어그램 보기" 시 "작업을 완료할 수 없습니다. 해당 인터페이스를 지원하지 않습니다." 오류 발생
11346정성태10/25/201719744오류 유형: 423. Windows Server 2003 - The client-side extension could not remove user policy settings for 'Default Domain Policy {...}' (0x8007000d)
11338정성태10/25/201717312.NET Framework: 697. windbg - SOS DumpMT의 "BaseSize", "ComponentSize" 값에 대한 의미파일 다운로드1
11337정성태10/24/201719858.NET Framework: 696. windbg - SOS DumpClass/DumpMT의 "Vtable Slots", "Total Method Slots", "Slots in VTable" 값에 대한 의미파일 다운로드1
11336정성태10/20/201721140.NET Framework: 695. windbg - .NET string의 x86/x64 메모리 할당 구조
11335정성태10/18/201719936.NET Framework: 694. 닷넷 - <Module> 클래스의 용도
11334정성태10/18/201720675디버깅 기술: 105. windbg - k 명령어와 !clrstack을 조합한 호출 스택을 얻는 방법
... 91  92  93  94  95  96  97  98  99  100  101  102  103  [104]  105  ...