liveBook · Manning

Number rounding

As explained in Learn Haskell by Example

round is a Haskell function that converts a fractional value to the nearest integral value according to its rounding rules.

In Haskell, round converts a fractional value into an integral value. Its type requires a RealFrac input and permits any Integral result, so Double or Float values can become Int or Integer values. section 10.1.1

Rounding is useful when a calculation produces a fractional quantity that the program must represent as a whole number, such as an audio sample count. section 10.1.1

How Learn Haskell by Example covers it

How deep it goes
Defines the RealFrac-to-Integral type relationship and demonstrates round in sample-count helpers and GHCi.
Examples
Uses audio sample-rate helpers and round (3.1415 :: Double) :: Integer as concrete examples.
What it assumes
Assumes basic arithmetic and introduces RealFrac and Integral as the relevant numeric type classes.
What you can do after
Apply round to convert floating-point calculations into values with an Integral result type.
How it connects
Connects RealFrac, Integral, Fractional, floating-point values, integer values, and sample rate calculations.

Also explained in

Compare how 4 books explain number rounding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.