formattable-0.1.1: Business-quality formatting of numbers, dates, and other things

CopyrightSoostone Inc
Safe HaskellNone




Formatting for numeric values.


Main number formatting functions and data types

formatNum :: Real a => NumFormat -> a -> Text Source

Primary function for formatting floating point numbers.

formatIntegral :: Integral a => NumFormat -> a -> Text Source

Primary function for formatting integrals. This was originally created to avoid depending on the double-conversion package which uses a C library and is therefore less portable. We're keeping this as a separate function because it should have the potential to be more efficient than the floating point version.

formatPct :: Real a => Int -> a -> Text Source

Convenience wrapper for percentages that lets you easily control the number of decimal places.

data NumFormat Source

The main data structure with all the necessary information for formatting numbers.




_nfUnits :: Double

Units of measure to use in formatting the number. This is useful for things like percentages where you would use (units of 0.01) or financial statements (units of 1000 for example).

_nfPrefix :: Text

A prefix to add to the number, commonly used for currency designation.

_nfSuffix :: Text

A suffix for the number. Percent, for example.

_nfThouSep :: Text

The character to use as thousands separator if applicable.

_nfDecSep :: Text

Character to use for the decimal separator.

_nfStyle :: NumStyle

The formatting style

_nfPrec :: Maybe (Int, PrecisionType)

Amount of precision to display

_nfNegStyle :: NegativeStyle

Styles for negative numbers

data NumStyle Source

Data structure describing available styles of number formatting.



Format with scientific notation


Format with standard decimal notation

SmartExponent Int Int

The aruments a and b define bounds. If the absolute value of the number is in the interval [10 ^ a, 10 ^ b], then it uses the Fixed style. If the number is outside this interval, then use the Exponent style.


Adds the symbol for the closest smaller SI prefix as the suffix to the formatted number. This suffix appears before any other suffix you designate.

SmartSI Double Double

Like SIStyle but only applies the SI prefix if the number to be formatted falls within the range [a,b] given by the SmartSI arguments.


autoStyle :: NumStyle Source

A reasonable default value for NumStyle.

data PrecisionType Source

Data structure for different methods of specifying precision.



Specifies precision as a fixed number of significant digits


Specifies precision with a fixed number of digits after the decimal place.

data NegativeStyle Source

Different styles for representing negative numbers.



Shows negative numbers as -123.000


Shows negative numbers as (123.000)

Common formats

rawIntFmt :: NumFormat Source

Int format with no thousands separator.

intFmt :: NumFormat Source

Int format with comma as the thousands separator.

percentFmt :: NumFormat Source

Common format for percentages. Example: 75.000%

numFmt :: NumFormat Source

Common format for generic numeric quantities of the form 123,456.99.

usdFmt :: NumFormat Source

Common format for US dollar quantities of the form $123,456.99.


Other formatting functions

formatNumGeneric Source


:: Real a 
=> (Maybe Int -> Double -> Text)

Exponential formatter

-> (Maybe Int -> Double -> Text)

Fixed formatter

-> NumFormat

Format specification

-> a

The number to format

-> Text 

Generic floating point formatting function that allows you to specify your own underlying functions for formatting exponential and fixed formats. This can allow you to use more efficient versions if available. We also use it the test suite to check behavior against the old double-conversion implementation.