gitwtfhub

wtf is decimal?

shopspring/decimal — explained in plain English

Analysis updated 2026-06-24

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

TL;DR

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

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

Why would anyone build with this?

REASON 1

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

REASON 2

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

REASON 3

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

REASON 4

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

What's in the stack?

Go

How it stacks up

shopspring/decimaltinyauthapp/tinyauthhackl0us/geoip2-cn
Stars7,3507,3517,336
LanguageGoGoGo
Setup difficultyeasymoderateeasy
Complexity2/54/52/5
Audiencedeveloperops devopsops devops

Figures from each repo's GitHub metadata at analysis time.

How do you spin it up?

Difficulty · easy Time to first run · 5min

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

Wtf does this do

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.

Yoink these 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.

Frequently asked questions

wtf is decimal?

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.

What language is decimal written in?

Mainly Go. The stack also includes Go.

How hard is decimal to set up?

Setup difficulty is rated easy, with roughly 5min to a first successful run.

Who is decimal for?

Mainly developer.

View the repo → Decode another repo

This repo across BitVibe Labs

Don't trust strangers blindly. Verify against the repo.