百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
U

udecimal

> 数据库
开源

一款用于金融应用的高性能、高精度、零分配的固定小数点库

193 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

一款用于金融应用的高性能、高精度、零分配的固定小数点库

udecimal

High performance, high precision, zero allocation fixed-point decimal number for financial applications.

Installation

Supported version: Go >= 1.23

bash
go get github.com/quagmt/udecimal

Features

  • High Precision: Supports up to 19 decimal places with no precision loss during arithmetic operations.
  • Zero Memory Allocation: Designed for almost 99% zero memory allocation (see How it works).
  • Optimized for Speed: 5x~20x faster than shopspring/decimal and ericlagergren/decimal (see Benchmark).
  • Panic-Free: All errors are returned as values, ensuring no unexpected panics.
  • Concurrent-Safe: All arithmetic operations return a new Decimal value while keeping the original value unchanged, making it safe to be shared across goroutines.
  • Correctness: All arithmetic operations are fuzz tested and cross-checked with shopspring/decimal library to ensure correctness.
  • Versatile Rounding Methods: Includes HALF AWAY FROM ZERO, HALF TOWARD ZERO, AWAY FROM ZERO, and Banker's rounding.

NOTE: This library does not perform implicit rounding. If the result of an operation exceeds the maximum precision, extra digits are truncated. All rounding methods must be explicitly invoked. (see Rounding Methods for more details)

Documentation

  • Checkout documentation for more information.

Usage

…

Why another decimal library?

There are already a couple of decimal libraries available in Go, such as shopspring/decimal, cockroachdb/apd, govalues/decimal, etc. However, each of these libraries has its own limitations, for example:

  • shopspring/decimal is great for general-purpose decimal arithmetic because of arbitrary precision. However, it's slow and requires memory allocation for every arithmetic operation. Also in financial applications, arbitrary precision is not always necessary.
  • cockroachdb/apd is faster but still requires memory allocation. Also the API is not very intuitive.
  • govalues/decimal is fast, no memory allocation, easy to use but the data range is only limited to 19 digits (include both the integer and fractional parts). Some operations (especially Quo/QuoRem) usually overflow and fallback to use big.Int API, which hurts the performance. Another limitation is that it starts losing precision when the total number of digits exceeds 19.

This library is designed to address these limitations, providing both high performance and zero allocation while maintaining an acceptable range of precision, which is suitable for most financial applications.

Rounding Methods

Rounding numbers can often be challenging and confusing due to the variety of methods available. Each method serves specific purposes, and it's common for developers to make mistakes or incorrect assumptions about how rounding should be performed. For example, the result of round(1.45) could be either 1.4 or 1.5, depending on the rounding method used.

This issue is particularly critical in financial applications, where even minor rounding errors can accumulate and lead to significant financial losses. To mitigate such errors, this library intentionally avoids implicit rounding. If the result of an operation exceeds the maximum precision specified by developers beforehand, extra digits are truncated. Developers need to explicitly choose the rounding method they want to use. The supported rounding methods are:

  • Banker's rounding or round half to even
  • Round away from zero
  • Half away from zero (HAZ)
  • Half toward zero (HTZ)

Examples:

go
package main

import (
	"fmt"

	"github.com/quagmt/udecimal"
)

func main() {
	// Create a new decimal number
	a, _ := udecimal.NewFromFloat64(1.345) // a = 1.345

	// Rounding
	fmt.Println(a.RoundBank(2))         // banker's rounding: 1.345 -> 1.34
	fmt.Println(a.RoundAwayFromZero(2)) // round away from zero: 1.345 -> 1.35
	fmt.Println(a.RoundHAZ(2))          // half away from zero: 1.345 -> 1.35
	fmt.Println(a.RoundHTZ(2))          // half towards zero: 1.345 -> 1.34
}

How it works

As mentioned above, this library is not always memory allocation free. However, those cases where we need to allocate memory are incredibly rare. To understand why, let's take a look at how the Decimal type is implemented.

The Decimal type represents a fixed-point decimal number. It consists of three components: sign, coefficient, and prec. The number is represented as:

go
// decimal value = (neg == true ? -1 : 1) * coef * 10^(-prec)
type Decimal struct {
	coef bint
	neg bool
	prec uint8 // 0  neg = false, coef = 123456, prec = 3

// -123.456 = -123456 / 10^-3
// -> neg = true, coef = 123456, prec = 3

You can notice that coef data type is bint, which is a custom data type:

go
type bint struct {
	// For coefficients exceeding u128
	bigInt *big.Int

	// For coefficients less than 2^128
	u128 u128
}

The bint type can store coefficients up to 2^128 - 1 using u128. Arithmetic operations with u128 are fast and require no memory allocation. If result of an arithmetic operation exceeds u128 capacity, the whole operation will be performed using big.Int API. Such operations are slower and do involve memory allocation. However, those cases are rare in financial applications due to the extensive range provided by a 128-bit unsigned integer, for example:

  • If precision is 0, the decimal range it can store is: [-340282366920938463463374607431768211455, 340282366920938463463374607431768211455](approximately -340 to 340 undecillion)

  • If precision is 19, the decimal range becomes: [-34028236692093846346.3374607431768211455, 34028236692093846346.3374607431768211455] (approximately -34 to 34 quintillion)

Therefore, in most cases you can expect high performance and no memory allocation when using this library.

Credits

This library is inspired by these repositories:

  • govalues/decimal
  • lukechampine/uint128
  • ridiculousfish/libdivide

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Godecimaldecimalsfinancialgo

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库