MissingH-1.0.3: Large utility librarySource codeContentsIndex
Data.Quantity
Portabilityportable
Stabilityprovisional
MaintainerJohn Goerzen <jgoerzen@complete.org>
Description

Tools for rendering sizes

Written by John Goerzen, jgoerzen@complete.org

Synopsis
renderNum :: (Ord a, Real a) => SizeOpts -> Int -> a -> String
renderNums :: (Ord a, Real a) => SizeOpts -> Int -> [a] -> [String]
parseNum :: (Read a, Fractional a) => SizeOpts -> Bool -> String -> Either String a
parseNumInt :: (Read a, Integral a) => SizeOpts -> Bool -> String -> Either String a
quantifyNum :: (Ord a, Real a, Floating b, Ord b) => SizeOpts -> a -> (b, Char)
quantifyNums :: (Ord a, Real a, Floating b, Ord b) => SizeOpts -> [a] -> ([b], Char)
data SizeOpts = SizeOpts {
base :: Int
powerIncr :: Int
firstPower :: Int
suffixes :: String
}
binaryOpts :: SizeOpts
siOpts :: SizeOpts
Documentation
renderNumSource
:: (Ord a, Real a)
=> SizeOptsPrecision of the result
-> IntThe number to examine
-> a
-> String

Render a number into a string, based on the given quantities. This is useful for displaying quantities in terms of bytes or in SI units. Give this function the SizeOpts for the desired output, and a precision (number of digits to the right of the decimal point), and you get a string output.

Here are some examples:

 Data.Quantity> renderNum binaryOpts 0 1048576
 "1M"
 Data.Quantity> renderNum binaryOpts 2 10485760
 "10.00M"
 Data.Quantity> renderNum binaryOpts 3 1048576
 "1.000M"
 Data.Quantity> renderNum binaryOpts 3 1500000
 "1.431M"
 Data.Quantity> renderNum binaryOpts 2 (1500 ** 3)
 "3.14G"
 Data.Quantity> renderNum siOpts 2 1024
 "1.02k"
 Data.Quantity> renderNum siOpts 2 1048576
 "1.05M"
 Data.Quantity> renderNum siOpts 2 0.001
 "1.00m"
 Data.Quantity> renderNum siOpts 2 0.0001
 "100.00u"

If you want more control over the output, see quantifyNum.

renderNumsSource
:: (Ord a, Real a)
=> SizeOptsPrevision of the result
-> IntThe numbers to examine
-> [a]Result
-> [String]

Like renderNum, but operates on a list of numbers. The first number in the list will be evaluated for the suffix. The same suffix and scale will be used for the remaining items in the list. See renderNum for more examples.

Also, unlike renderNum, the %f instead of %g printf format is used so that "scientific" notation is avoided in the output.

Examples:

 *Data.Quantity> renderNums binaryOpts 3 [1500000, 10240, 104857600]
 ["1.431M","0.010M","100.000M"]
 *Data.Quantity> renderNums binaryOpts 3 [1500, 10240, 104857600]
 ["1.465K","10.000K","102400.000K"]
parseNumSource
:: (Read a, Fractional a)
=> SizeOptsWhether to perform a case-insensitive match
-> BoolThe string to parse
-> String
-> Either String a

Parses a String, possibly generated by renderNum. Parses the suffix and applies it to the number, which is read via the Read class.

Returns Left error message on error, or Right number on successful parse.

If you want an Integral result, the convenience function parseNumInt is for you.

parseNumIntSource
:: (Read a, Integral a)
=> SizeOptsWhether to perform a case-insensitive match
-> BoolThe string to parse
-> String
-> Either String a

Parse a number as with parseNum, but return the result as an Integral. Any type such as Integer, Int, etc. can be used for the result type.

This function simply calls round on the result of parseNum. A Double is used internally for the parsing of the numeric component.

By using this function, a user can still say something like 1.5M and get an integral result.

quantifyNum :: (Ord a, Real a, Floating b, Ord b) => SizeOpts -> a -> (b, Char)Source
Takes a number and returns a new (quantity, suffix) combination. The space character is used as the suffix for items around 0.
quantifyNums :: (Ord a, Real a, Floating b, Ord b) => SizeOpts -> [a] -> ([b], Char)Source

Like quantifyNum, but takes a list of numbers. The first number in the list will be evaluated for the suffix. The same suffix and scale will be used for the remaining items in the list. Please see renderNums for an example of how this works.

It is invalid to use this function on an empty list.

data SizeOpts Source
The options for quantifyNum and renderNum
Constructors
SizeOpts
base :: IntThe base from which calculations are made
powerIncr :: IntThe increment to the power for each new suffix
firstPower :: IntThe first power for which suffixes are given
suffixes :: StringThe suffixes themselves
binaryOpts :: SizeOptsSource
Predefined definitions for byte measurement in groups of 1024, from 0 to 2**80
siOpts :: SizeOptsSource
Predefined definitions for SI measurement, from 10**-24 to 10**24.
Produced by Haddock version 2.6.0