decimals

package module
v0.0.0-...-ddba745 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 25, 2015 License: MIT Imports: 2 Imported by: 1

README

decimals

Decimals is a small library of functions for rounding and formatting base ten numbers in Go. These are functions that are either missing from the standard libraries or are more convenient for presenting numbers in a human-readable format.

Version 2.0

In the original version of this package, integer rounding was performed with mathematical operations. This worked in most cases but produced incorrect results when rounding at or near the minimum and maximum int64 values. The latest version of the library provides the same public interface, but internally uses string representations of decimal numbers in order to produce consistent rounding behaviour across the full range of integer values. If you attempt to round to a number beyond the minimum or maximum int64 values the minimum or maximum is returned instead.

Installation

Install with go get.

go get github.com/olihawkins/decimals
Tests

Use go test to run the tests.

Documentation

See the GoDoc for the full documentation.

Rounding

Round int64 and float64 numbers.

decimals.RoundInt(x int64, precision int) int64
decimals.RoundFloat(x float64, precision int) float64

Round an integer to the nearest power of ten using a negative value for precision.

i := decimals.RoundInt(555, 0)  // i = 555
i := decimals.RoundInt(555, -1) // i = 560
i := decimals.RoundInt(555, -2) // i = 600
i := decimals.RoundInt(555, -3) // i = 1000 

Round a float to the given number of decimal places using a positive value for precision, or to the nearest power of ten using a negative value for precision.

f := decimals.RoundFloat(5.5555, 3)  // f = 5.556
f := decimals.RoundFloat(5.5555, 2)  // f = 5.56
f := decimals.RoundFloat(5.5555, 1)  // f = 5.6
f := decimals.RoundFloat(5.5555, 0)  // f = 6
f := decimals.RoundFloat(5.5555, -1) // f = 10
Formatting

Convert integers and floats to formatted strings with the given decimal precision, using a comma separator for thousands.

decimals.FormatInt(x int64, precision int) string
decimals.FormatFloat(x float64, precision int) string

Format an integer rounded to the nearest power of ten using a negative value for precision.

i := decimals.FormatInt(5555555, 0)  // i = "5,555,555"
i := decimals.FormatInt(5555555, -1) // i = "5,555,560"
i := decimals.FormatInt(5555555, -2) // i = "5,555,600"
i := decimals.FormatInt(5555555, -3) // i = "5,556,000" 

Format a float rounded to the given number of decimal places using a positive value for precision, or to the nearest power of ten using a negative value for precision.

f := decimals.FormatFloat(5555.555, 2)  // f = "5,555.56"
f := decimals.FormatFloat(5555.555, 1)  // f = "5,555.6"
f := decimals.FormatFloat(5555.555, 0)  // f = "5,556"
f := decimals.FormatFloat(5555.555, -1) // f = "5,560"
f := decimals.FormatFloat(5555.555, -2) // f = "5,600"

Documentation

Overview

Package decimals is a small library of functions for rounding and formatting base ten numbers. These are functions that are either missing from the standard libraries or are more convenient for presenting numbers in a human-readable format.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatFloat

func FormatFloat(x float64, precision int) string

FormatFloat converts a float64 to a formatted string. The float is rounded to the given precision and formatted using a comma separator for thousands.

func FormatInt

func FormatInt(x int64, precision int) string

FormatInt converts an int64 to a formatted string. The int is rounded to the given precision and formatted using a comma separator for thousands.

func FormatThousands

func FormatThousands(x int64) string

FormatThousands converts an int64 into a string formatted using a comma separator for thousands.

func RoundFloat

func RoundFloat(x float64, precision int) float64

RoundFloat rounds a base ten float64 to the given decimal precision. Precision may be positive, representing the number of decimal places, or negative, representing the nearest power of ten to which the float should be rounded.

func RoundInt

func RoundInt(x int64, precision int) int64

RoundInt rounds a base ten int64 to the given precision. Precision is a negative number that represents the nearest power of ten to which the integer should be rounded. It is expressed as a negative number to be consistent with the decimal precision arguments used in rounding floats. If the rounded number falls outside the minimum and maximum for int64 the minimum or maximum will be returned instead.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL