explaingit

shopspring/decimal

7,350GoAudience · developerComplexity · 2/5Setup · easy

TLDR

A Go library for precise decimal arithmetic that eliminates the rounding errors ordinary floating-point numbers produce, making it safe to handle money, prices, and financial totals in Go applications.

Mindmap

mindmap
  root((decimal))
    What it does
      Precise arithmetic
      No rounding errors
      Money calculations
    Operations
      Add and subtract
      Multiply
      Divide with precision
    Serialization
      JSON
      XML
      SQL databases
    Design
      Immutable values
      No shared state bugs
    Audience
      Go developers
      Fintech apps
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Calculate shopping cart totals, tax amounts, or invoice line items in Go without cents disappearing due to floating-point rounding errors.

USE CASE 2

Split a bill precisely among multiple people and verify that every fraction of a cent is accounted for with no money lost.

USE CASE 3

Store and retrieve money values from a SQL database, JSON API, or XML file without writing custom serialization code.

USE CASE 4

Divide financial amounts at a user-specified precision level when you need to control exactly how many decimal places a result carries.

Tech stack

Go

Getting it running

Difficulty · easy Time to first run · 5min

Requires Go 1.10 or later, install with the standard go get command, no external infra needed.

In plain English

This is a Go library for working with decimal numbers at arbitrary precision. It exists to solve a problem that trips up many programs handling money: ordinary floating-point numbers in computers cannot represent values like 0.1 exactly. When you do repeated arithmetic with those numbers, small rounding errors accumulate and, in a financial context, that means cents or fractions of cents can appear or disappear from totals. The library's decimal type supports addition, subtraction, and multiplication with no loss of precision. Division is supported with a precision level you specify. The README's FAQ walks through two concrete examples: a simple subtraction using standard Go float64 that produces 9.999999999999831 instead of 10, and a money-splitting scenario where the standard Go big.Rat type loses track of a fraction of a cent across three transactions. The API is immutable: every method call returns a new decimal value and leaves the original unchanged. This design avoids a class of subtle bugs where a variable is accidentally modified through a shared reference. The README acknowledges the trade-off: immutability requires more memory allocations, so this library is slower than some alternatives. The assumption stated in the README is that code dealing with decimal numbers usually cares more about correctness than raw speed. The library supports serialization to and from JSON, XML, and SQL databases without requiring any custom conversion code. It requires Go version 1.10 or later and is available via the standard go get command. The README also links to four alternative libraries for situations where different trade-offs suit better: one optimized for arbitrary-precision performance similar to Go's big.Int, one with a compatible API but limited to 12 decimal digits, one with zero memory allocations at 19-digit precision, and one fork focused on billing and e-commerce cases with built-in BSON support.

Copy-paste prompts

Prompt 1
Using the shopspring/decimal library in Go, write a function that calculates the total cost of items in a shopping cart with tax applied, ensuring no rounding errors. Show the full example with go get installation.
Prompt 2
I need to split $100 three ways as evenly as possible in Go. Use shopspring/decimal to show how to divide and verify that all three shares add up to exactly $100.00 with no fractional cent lost.
Prompt 3
Show me how to use shopspring/decimal to read a JSON API response containing price strings like '19.99', do arithmetic on them, and write the result back to a database using the built-in SQL support.
Prompt 4
Compare shopspring/decimal to standard Go float64 arithmetic for a financial use case, showing the actual wrong output float64 produces versus the correct output decimal gives.
Open on GitHub → Explain another repo

← shopspring on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.