HasBigDecimal-0.1.1: A library for arbitrary precision decimal numbers.

Safe HaskellSafe
LanguageHaskell2010

Data.BigDecimal

Description

This module defines the type BigDecimal which provides a representation of arbitrary precision decimal numbers. BigDecimal is a native Haskell implementation based on arbitrary sized Integer values. The implementation was inspired by Java BigDecimals.

BigDecimal instantiates the typeclasses Num, Fractional and Real. It is thus possible to use all common operators like +, -, *, /, ^ on them.

Here are a few examples from an interactive GHCI session:

 λ> a = BigDecimal 144 2
 λ> toString a
 "1.44"
 λ> b = sqrt a
 λ> toString b
 "1.2"
 λ> b * b
 BigDecimal 144 2
 λ> b * b * b
 BigDecimal 1728 3
 λ> b^2
 BigDecimal 144 2
 λ> c = fromString "123.4567890"
 λ> c
 BigDecimal 1234567890 7
 λ> a / c
 BigDecimal 1166400010614240096589584878965222398584 41
 λ> roundBD it (halfUp 10)
 BigDecimal 116640001 10
 λ> divide (a, c) $ halfUp 20
 BigDecimal 1166400010614240097 20

Synopsis

Documentation

data BigDecimal Source #

BigDecimal is represented by an unscaled Integer value plus a second Integer value that defines the scale E.g.: (BigDecimal 1234 2) represents the decimal value 12.34.

Constructors

BigDecimal Integer Integer

creates a BigDecimal value from an unscaled Integer value and a scale, given as a positive Integer. Example: (BigDecimal 1234 2) creates the value 12.34

data RoundingMode Source #

RoundingMode defines how to handle loss of precision in divisions or explicit rounding.

Constructors

UP

Rounding mode to round away from zero.

DOWN

Rounding mode to round towards zero.

CEILING

Rounding mode to round towards positive infinity.

FLOOR

Rounding mode to round towards negative infinity.

HALF_UP

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.

HALF_DOWN

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.

HALF_EVEN

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.

PRECISE

Rounding mode to assert that the requested operation has an exact result, hence no rounding is applied.

type MathContext = (RoundingMode, Maybe Integer) Source #

A MathContext is interpreted by divisions and rounding operations to specify the expected loss of precision and the rounding behaviour. MathContext is a pair of a RoundingMode and a target precision of type Maybe Integer. The precision defines the number of digits after the decimal point. If Nothing is given as precision all decimal digits are to be preserved, that is precision is not limited.

getScale :: BigDecimal -> Integer Source #

gets the scale part of a BigDecimal

getValue :: BigDecimal -> Integer Source #

get the unscaled value of a BigDecimal

precision :: BigDecimal -> Integer Source #

returns the number of digits of an Integer

trim :: Integer -> BigDecimal -> BigDecimal Source #

removes trailing zeros from a BigDecimals intValue by decreasing the scale

nf :: BigDecimal -> BigDecimal Source #

computes the normal form of a BigDecimal

divide Source #

Arguments

:: (BigDecimal, BigDecimal)

the tuple of dividend and divisor. I.e. (dividend, divisor)

-> MathContext

MathContext (i.e. a tuple of RoundingMode and the specified precision) defines the rounding behaviour. if Nothing if given as precision the maximum possible precision is used.

-> BigDecimal

the resulting BigDecimal

divide two BigDecimals and applies the MathContext (i.e. a tuple of RoundingMode and the specified precision) for rounding.

roundBD :: BigDecimal -> MathContext -> BigDecimal Source #

round a BigDecimal to n digits applying the MathContext mc

fromRatio :: Rational -> MathContext -> BigDecimal Source #

creates a BigDecimal from a Rational value. MathContext defines precision and rounding mode.

halfUp :: Integer -> MathContext Source #

construct a MathContext for rounding HALF_UP with scale decimal digits

fromString :: String -> BigDecimal Source #

read a BigDecimal from a human readable decimal notation. e.g. fromString "3.14" yields 'BigDecimal 314 2'

matchScales :: (BigDecimal, BigDecimal) -> (BigDecimal, BigDecimal) Source #

match the scales of a tuple of BigDecimals

toString :: BigDecimal -> String Source #

returns a readable String representation of a BigDecimal e.g. toString (BigDecimal 314 2) yields "3.14"