Coder Social home page Coder Social logo

nqthqn / elm-round Goto Github PK

View Code? Open in Web Editor NEW

This project forked from myrho/elm-round

0.0 2.0 0.0 47 KB

Round floats (mathematically/commercially) to a given number of decimal places

Home Page: http://package.elm-lang.org/packages/myrho/elm-round/latest

License: BSD 3-Clause "New" or "Revised" License

Elm 100.00%

elm-round's Introduction

elm-round

This library converts a Float to a String with ultimate control how many digits after the decimal point are shown and how the remaining digits are rounded. It rounds, floors and ceils the "common" way (ie. half up) or the "commerical" way (ie. half away from zero).

Example:

x = 3.141592653589793

round 2 x -- "3.14"
round 4 x -- "3.1416"

ceiling 2 x -- "3.15"
floor 4 x -- "3.1415"

The given number of digits after decimal point can also be negative.

x = 213.14

round -2 x -- "200"
round -1 x -- "210"

ceiling -2 x -- "300"
floor -3 x -- "0"

Commercial rounding means that negative and positive numbers are treated symmetrically. It affects numbers whose last digit equals 5. For example:

x = -0.5

round 0 x -- "0"
roundCom 0 x -- "-1"

floor 0 x -- "-1"
floorCom 0 x -- "0"

ceiling 0 x -- "0"
ceilingCom 0 x -- "-1"

Have a look at the tests for more examples!

Why couldn't you just do x * 1000 |> round |> toFloat |> (flip (/)) 1000 in order to round to 3 digits after comma? Due to floating point arithmetic it might happen that it results into someting like 3.1416000000001, although we just wanted 3.1416.

Under the hood this library converts the Float into a String and rounds it char-wise. Hence it's safe from floating point arithmetic weirdness.

Installation

From the root of your Elm project run

elm package install myrho/elm-round

Import it in your Elm modules:

import Round 

Releases

Version Notes
1.0.4 Upgrade to Elm 0.19
1.0.3 Fix issues with number in scientific notation, complete rewrite.
1.0.2 Given number of digits after decimal point can be negative.
1.0.1 Upgrade to Elm 0.18
1.0.0 First official release, streamlined API and tests, docs added

elm-round's People

Contributors

myrho avatar terezka avatar

Watchers

James Cloos avatar Nate avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.